-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio_utils.py
31 lines (25 loc) · 864 Bytes
/
io_utils.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
28
29
30
31
import numpy as np
def read_textfile(filename, step=0):
"""
Read a textfile and return a list of lines
:param filename: the path to the textfile
:param step: the number of lines per example (for example, if step=4, then every two lines are grouped together)
:return: a list of lines
"""
with open(filename, "r") as f:
textfile = f.readlines()
# reshape to 2D array
if step:
textfile = np.array(textfile).reshape((-1, step)).tolist()
return textfile
def cont_write_textfile(data, output_path):
"""
Write data to a textfile in a continuous manner
:param data: a list of lines
:param output_path: the path to the output textfile
:return: None
"""
with open(output_path, "a", encoding='utf-8') as f:
for line in data:
f.write(line)
f.write("\n")