8000 Python How to Wrap Long Lines in Text file · yeonathan/python@a2c8a0e · GitHub
[go: up one dir, main page]

Skip to content

Commit a2c8a0e

Browse files
committed
Python How to Wrap Long Lines in Text file
1 parent 40826ab commit a2c8a0e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

scripts/1.python_wrap_lines.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
3+
size = 80
4+
file = 'budo'
5+
folder = os.path.expanduser('~/Documents/Fortunes/')
6+
7+
# Read and store the entire file line by line
8+
with open(f'{folder}{file}.txt') as reader:
9+
provers = reader.readlines()
10+
11+
# wrap/collate lines by separators [",", " ", "."]
12+
def collate(text, size):
13+
new_text = []
14+
split_char = 1
15+
while split_char > 0:
16+
comma = str.find(text, ',', size)
17+
space = str.find(text, ' ', size)
18+
dot = str.find(text, '.', size)
19+
20+
split_char = min(max(comma, dot), max(comma, space), max(dot, space))
21+
22+
if text[:split_char]:
23+
new_text.append(text[:split_char])
24+
text = text[split_char+1:].replace('\n', "")
25+
26+
return new_text
27+
28+
# write collated information to new(same) file
29+
with open(f'{folder}{file}.txt', 'w') as writer:
30+
for wisdom in provers:
31+
if len(wisdom) > size:
32+
collated = collate(wisdom, size)
33+
for short in collated:
34+
writer.write(short)
35+
writer.write('\n')
36+
else:
37+
writer.write(wisdom)
38+
39+
# Executing Shell Commands with Python
40+
import os
41+
myCmd = f'strfile -c % {folder}{file}.txt {folder}{file}.txt.dat'
42+
os.system(myCmd)

0 commit comments

Comments
 (0)
0