-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting-test.py
25 lines (22 loc) · 1.15 KB
/
formatting-test.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
import textwrap
dc = {'really, really, really, really, really, really, really looooooooooooooooong': 2, 'short': 1, 'medium length key' : 134, 'pretty kinda sorta long but not as long as others' : 56}
with open('C:/temp/textwrap.txt', 'w') as f:
for k in dc.keys():
if len(k) < 30:
diff = 30 - len(k)
spaces = len(k) + diff
value = k.rjust(spaces, ' ')
#print('Too short:\noriginal: {}\nLength: {}\nNew value: *{}*\n\n\n'.format(k, len(k), value))
elif len(k) > 30:
content = textwrap.wrap(k, 30)
for i, c in enumerate(content):
if len(c) < 30:
diff = 30 - len(c)
spaces = len(c) + diff
content[i] = c.rjust(spaces, ' ')
value = '\n'.join(content)
#print('Too long:\noriginal: {}\nLength: {}\nNew value: *{}*\n\n\n'.format(k, len(k), temp_item))
else:
value = k
#print('Just right:\noriginal: {}\nLength: {}\nNew value: *{}*\n\n\n'.format(k, len(k), value))
f.write("{} : {}\n\n".format(value, dc[k]))