-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbreak_line.py
More file actions
50 lines (37 loc) · 1.04 KB
/
Copy pathbreak_line.py
File metadata and controls
50 lines (37 loc) · 1.04 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
- Read from a text file (alice.txt) and print lines in a lenght of N.
- No word should be broken in different lines, this means, If the n-th is
in the middle of a word, the complete word should be printed in the same line.
"""
def break_lines(string, n=40, strip_original_linebreaks=True):
'''
>>> break_lines('asdf asdf', 6)
asdf asdf
>>> break_lines('asdf asdf', 4)
asdf
asdf
>>> break_lines('a s d f', 1)
a
s
d
f
'''
linhas = []
k = 0
if strip_original_linebreaks:
string = string.replace('\n', ' ')
string_quebrada = string.split(' ')
for palavra in string_quebrada:
if not linhas:
linhas.append(string_quebrada[0])
continue
if len(linhas[k]) + 1 < n:
linhas[k] += ' ' + palavra
else:
k += 1
linhas.append(palavra)
print("\n".join(linhas))
if __name__ == "__main__":
with open('alice.txt', encoding='UTF-8') as f:
texto = f.read()
break_lines(texto, 40)