-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
32 lines (23 loc) · 868 Bytes
/
common.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
def file_to_lines(filename, separate_with_empty=False):
lines = []
if not separate_with_empty:
with open(filename, 'r') as file:
lines = file.readlines()
return lines
with open(filename, 'r') as file:
# row may consist of data from multiple lines
current_row = []
for line in file:
line_stripped = line.strip()
if len(line_stripped):
current_row.append(line_stripped)
else:
# we found an empty line
row_joined = ' '.join(current_row)
lines.append(row_joined)
current_row = []
# add the last missing row that had no chance of being added
if len(current_row):
row_joined = ' '.join(current_row)
lines.append(row_joined)
return lines