summaryrefslogtreecommitdiff
path: root/util.py
diff options
context:
space:
mode:
authoradamhrv <adam@ahprojects.com>2018-11-04 21:54:00 +0100
committeradamhrv <adam@ahprojects.com>2018-11-04 21:54:00 +0100
commit9bcba0d02aafb34a5a9ca3db2f894f1fc95401c0 (patch)
tree3dcaf94563498c15b56d51efc62750d0be72e01a /util.py
parentef45f3c93ffd39b57ee56db74a95f9d2dae074a8 (diff)
parent0dc3e40434c23e4d48119465f39b03bf35fb56bd (diff)
.
Diffstat (limited to 'util.py')
-rw-r--r--util.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/util.py b/util.py
new file mode 100644
index 00000000..dae3f67c
--- /dev/null
+++ b/util.py
@@ -0,0 +1,31 @@
+import os
+import csv
+import simplejson as json
+
+def read_citation_list(index=0):
+ filename = './datasets/citations.csv'
+ if index > 0:
+ fn, ext = os.path.splitext(filename)
+ filename = fn + '-' + str(index) + ext
+ with open(filename, 'r') as f:
+ reader = csv.reader(f)
+ lines = list(reader)
+ keys = lines[0]
+ lines = lines[1:]
+ return keys, lines
+
+def read_json(fn):
+ with open(fn, 'r') as json_file:
+ return json.load(json_file)
+
+def write_json(fn, data):
+ with open(fn, 'w') as outfile:
+ json.dump(data, outfile)
+
+def write_csv(fn, keys, rows):
+ with open(fn, 'w') as f:
+ writer = csv.writer(f)
+ if keys is not None:
+ writer.writerow(keys)
+ for row in rows:
+ writer.writerow(row)