forked from gto76/python-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_table.py
More file actions
27 lines (27 loc) · 1.12 KB
/
convert_table.py
File metadata and controls
27 lines (27 loc) · 1.12 KB
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
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()