forked from gto76/python-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_table.py
executable file
·39 lines (36 loc) · 1.33 KB
/
convert_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
def convert_table(lines):
def from_ascii():
out = []
first, header, third, *body, last = lines
first = first.translate(str.maketrans({'-': '━', '+': '┯'}))
out.append(f'┏{first[1:-1]}┓')
header = header.translate(str.maketrans({'|': '│'}))
out.append(f'┃{header[1:-1]}┃')
third = third.translate(str.maketrans({'-': '─', '+': '┼'}))
out.append(f'┠{third[1:-1]}┨')
for line in body:
line = line.translate(str.maketrans({'|': '│'}))
line = line.replace('yes', ' ✓ ')
out.append(f'┃{line[1:-1]}┃')
last = last.translate(str.maketrans({'-': '━', '+': '┷'}))
out.append(f'┗{last[1:-1]}┛')
return '\n'.join(out)
def from_unicode():
out = []
for line in lines:
line = line.translate(str.maketrans('┏┓┗┛┠┼┨┯┷━─┃│', '+++++++++--||'))
line = line.replace(' ✓ ', 'yes')
out.append(line)
return '\n'.join(out)
if lines[0][0] == '+':
return from_ascii()
return from_unicode()
if __name__ == '__main__':
input_lines = []
try:
while True:
input_lines.append(input())
except EOFError:
pass
print(convert_table(input_lines))