From c2f4665dbe5ff1225f90afbaf590975057dc5026 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 3 Nov 2018 01:13:49 +0100 Subject: s2 dump scripts... --- util.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 util.py (limited to 'util.py') 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) -- cgit v1.2.3-70-g09d2