-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_io.py
304 lines (250 loc) · 9.39 KB
/
file_io.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import csv
import sys
import time as custom_time
import json
from numpyencoder import NumpyEncoder
maxInt = sys.maxsize
while True: # decrease the maxInt value by factor 10 as long as the OverflowError occurs
try:
csv.field_size_limit(maxInt)
break
except OverflowError:
maxInt = int(maxInt/10)
def write_data_to_csv_file(file_name, header_list, data_dict):
try:
file = open(file_name, 'a', newline = '', encoding = 'utf-8')
with file:
writer = csv.DictWriter(file, fieldnames = header_list)
#writer.writeheader()
writer.writerow(data_dict)
except Exception as e:
print('Error -- write_data_to_csv_file: ', e)
pass
def write_to_text_file(file_name, data):
"""
write a text in a new line to file
file_name: string - file name
data: string
"""
try:
with open(file_name, 'a', encoding='utf-8') as f:
f.write(data + '\n')
f.close()
except Exception as e:
print('Error -- write_to_text_file: ', e)
def write_to_new_text_file(file_name, data):
"""
write text into a new file
file_name: string - file name
data: string
"""
try:
with open(file_name, 'w', encoding='utf-8') as f:
if (data == ''):
f.write(data)
else:
f.write(data + '\n')
f.close()
except Exception as e:
print('Error -- write_to_new_text_file: ', e)
def write_list_to_json_file(file_name, data_list, file_access = 'a'):
"""
write to json file, append new list
data_list: list - list of data
"""
try:
with open(file_name, file_access, encoding='utf-8') as outfile:
json.dump(data_list, outfile, indent=4, separators=(', ', ': '), ensure_ascii=False, cls=NumpyEncoder)
outfile.close()
except Exception as e:
print('Error -- write_list_to_json_file: ', e)
def write_list_to_jsonl_file(file_name, data_list, file_access = 'a'):
"""
write to json file, append new list
data_list: list - list of data
"""
try:
with open(file_name, file_access, encoding='utf-8') as outfile:
for item in data_list:
#print(item)
json.dump(item, outfile, separators=(', ', ': '), ensure_ascii=False, cls=NumpyEncoder)
outfile.write('\n')
outfile.close()
except Exception as e:
print('Error -- write_list_to_jsonl_file: ', e)
def write_list_to_tsv_file(file_name, data_list, delimiter = '\t', file_access = 'a', quoting = csv.QUOTE_NONE):
"""
write to json file, append new list
data_list: list - list of data
"""
try:
with open(file_name, file_access, encoding='utf-8', newline='') as outfile:
for item in data_list:
data_item = [v for k, v in item.items()] # convert to value list
tsv_output = csv.writer(outfile, delimiter=delimiter, lineterminator='\n', quoting=quoting)
tsv_output.writerow(data_item)
outfile.close()
except Exception as e:
print('Error -- write_list_to_tsv_file: ', e)
def write_single_dict_to_jsonl_file(out_file_name, data_dict, try_no = 0, file_access = 'a', format_json = False):
"""
write list to *.json file
out_file_name: string - file name
data_dict: dict - a single data dictionary
try_no: int - the number of times to try to write
return: void
"""
try:
data_string = json.dumps(data_dict, separators=(', ', ': '), ensure_ascii=False, cls=NumpyEncoder)
with open(out_file_name, file_access, encoding='utf-8') as outfile:
if (format_json == False):
outfile.write(data_string + '\n')
else:
outfile.write(data_string)
outfile.close()
return True
except Exception as e:
print('Error -- write_single_dict_to_json_file: ', e)
try_no += 1
if (try_no <= 10):
return write_single_dict_to_jsonl_file(out_file_name, data_dict, try_no, file_access) # try to read file again
return False
def write_single_dict_to_json_file(out_file_name, data_dict, try_no = 0, file_access = 'a', format_json = False):
"""
write list to *.json file
out_file_name: string - file name
data_dict: dict - a single data dictionary
try_no: int - the number of times to try to write
return: void
"""
try:
# indent=4,
data_string = json.dumps(data_dict, separators=(', ', ': '), ensure_ascii=False, cls=NumpyEncoder)
with open(out_file_name, file_access, encoding='utf-8') as outfile:
if (format_json == False):
outfile.write(data_string + ',\n')
else:
outfile.write(data_string)
outfile.close()
return True
except Exception as e:
print('Error -- write_single_dict_to_json_file: ', e)
try_no += 1
if (try_no <= 10):
return write_single_dict_to_json_file(out_file_name, data_dict, try_no, file_access) # try to read file again
return False
def read_list_from_json_file(out_file_name, format_json = True, try_no = 0):
"""
load list from *.json file
out_file_name: string - file name
return: list - a return list
"""
result_list = []
try:
with open(out_file_name, 'r', encoding='utf-8') as outfile:
text = outfile.read()
text = text.strip(',\n') # remove the start & last syntaxes
if (format_json == False):
result_list = json.loads('[' + text + ']')
else:
result_list = json.loads(text)
outfile.close()
except Exception as e:
print('Error -- read_list_from_json_file: ', e)
try_no += 1
if (try_no <= 10):
custom_time.sleep(2)
return read_list_from_json_file(out_file_name, format_json, try_no) # try to read file again
pass
return result_list
def read_list_from_jsonl_file(out_file_name, try_no = 0):
"""
load list from *.json file
out_file_name: string - file name
return: list - a return list
"""
result_list = []
i = 0
try:
with open(out_file_name, 'r', encoding='utf-8') as outfile:
for line in outfile:
item = json.loads(line)
result_list.append(item)
i += 1
outfile.close()
except Exception as e:
print('Error -- read_list_from_jsonl_file: ', e, '-- check line: ', i)
try_no += 1
if (try_no <= 10):
custom_time.sleep(2)
return read_list_from_jsonl_file(out_file_name, try_no) # try to read file again
pass
return result_list
def write_list_to_text_file(file_name, data_list, file_access = 'a'):
"""
write a text in a new line to file
file_name: string - file name
data: string
"""
try:
data = ''
for item in data_list:
data += item + '\n'
with open(file_name, file_access, encoding='utf-8') as f:
f.write(data)
f.close()
except Exception as e:
print('Error -- write_list_from_text_file: ', e)
def read_list_from_text_file(file_name):
"""
read from text file
file_name: string - file name
"""
page_list = []
try:
with open(file_name, 'r', encoding='utf-8') as f:
for line in f:
page_list.append(line.strip())
#print(line)
f.close()
except:
print('Error -- read_list_from_text_file: ', e)
with open(file_name, 'a', encoding='utf-8') as f: # create a new empty file
f.close()
if (len(page_list) == 1):
return page_list[0] # return the first element in list
return page_list
def read_from_text_file(file_name):
"""
write a text in a new line to file
file_name: string - file name
data: string
"""
data = ''
try:
with open(file_name, 'r', encoding='utf-8') as f:
data = f.read()
f.close()
except:
pass
return data
def read_list_from_csv_file(file_name, delimiter = ',', encoding = 'utf-8'):
"""
write a text in a new line to file
file_name: string - file name
return: list
"""
data_list = []
try:
with open(file_name, 'r', encoding=encoding) as f:
file = csv.reader(f, delimiter = delimiter)
for line in file:
data_list.append(line)
f.close()
except Exception as e:
print('Error -- read_list_from_csv_file: ', e)
with open(file_name, 'a', encoding=encoding) as f: # create a new empty file
f.close()
if (len(data_list) == 1):
return data_list[0] # return the first element in list
return data_list