-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
36 lines (29 loc) · 999 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
32
33
34
35
36
import csv
import json
def readCSV(filename):
''' Read an CSV file and returns a list of ordered dicts based on its header '''
try:
file_obj = open(filename)
except IOError:
error_message = f"{filename} not found"
print(error_message)
exit()
csvDictsList = list(csv.DictReader(file_obj, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL, skipinitialspace=True))
return csvDictsList
def readJSON(filename):
try:
with open(filename, 'r') as file_obj:
return json.load(file_obj)
except:
return None
def writeJSON(data, filename):
with open(filename, 'w') as file_obj:
json.dump(data, file_obj)
def appendJSON(filename, new_data):
with open(filename) as file_obj:
try:
old_data = json.load(file_obj)
old_data.update(new_data)
except: #In case there is a .json file but its empty
old_data = new_data
writeJSON(old_data, filename)