-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
31 lines (26 loc) · 902 Bytes
/
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 csv
import pickle
def read_from_csv_file(filename):
rows = []
with open(filename, encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
rows.append(row)
return rows
def write_to_csv_file(filename, fieldnames, data):
with open(filename, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(fieldnames)
writer.writerows(data)
def write_list_of_dicts_to_csv_file(filename, data):
with open(filename, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, data[0].keys())
writer.writeheader()
writer.writerows(data)
def write_object_to_pickle_file(filename, data):
with open(filename, 'wb') as f:
pickle.dump(data, f)
def read_pickle_file(filename):
with open(filename, 'rb') as f:
obj = pickle.load(f)
return obj