-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.py
42 lines (33 loc) · 1.14 KB
/
parser.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
import csv
import json
# Catalogue headers
fieldnames = ('frb', 'utc', 'mjd', 'telescope', 'ra', 'dec', 'l', 'b', 'frequency', 'dm', 'flux', 'width', 'fluence', 'snr', 'ref', 'redshift', 'redshift_measured', 'ra_error', 'dec_error', 'dm_error')
# Load CSV catalogue
with open('catalogue.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile, fieldnames)
header = reader.fieldnames
# Write to JSON
with open('catalogue.json', 'w') as jsonfile:
# Prepend brace
jsonfile.write('[\n')
first = True
for row in reader:
if first:
# Skip header
first = False
continue
else:
json.dump(row, jsonfile)
jsonfile.write('\n')
# Append brace
jsonfile.write(']')
# Read json file
with open('catalogue.json', 'r') as f:
filedata = f.read()
# Append comma separators
filedata = filedata.replace('}\n{', '},\n{').replace('}{', '},{')
# Replace blanks (empty cells) with 'x'
filedata = filedata.replace('""', '"x"')
# Output parsed json
with open('catalogue.json', 'w') as file:
file.write(filedata)