-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsontocsv.py
62 lines (49 loc) · 1.4 KB
/
jsontocsv.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import json
import csv
##
# Convert to string keeping encoding in mind...
##
def to_string(s):
try:
return str(s)
except:
# Change the encoding type if needed
return s.encode('utf-8')
def merge_two_dicts(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z
def move():
# Reading arguments
node = "features"
name = "pam_2010"
json_file_path = name+".json"
csv_file_path = name+".csv"
fp = open(json_file_path, 'r')
json_value = fp.read()
raw_data = json.loads(json_value)
try:
data_to_be_processed = raw_data[node]
except:
data_to_be_processed = raw_data
processed_data = []
header = []
first = True
for item in data_to_be_processed:
attr = item['attributes']
geo = item['geometry']
reduced_item = merge_two_dicts(attr, geo)
# if first:
header += reduced_item.keys()
# first = False
processed_data.append(reduced_item)
header = list(set(header))
header.sort()
with open(csv_file_path, 'w+') as f:
writer = csv.DictWriter(f, header, quoting=csv.QUOTE_ALL)
writer.writeheader()
for row in processed_data:
writer.writerow(row)
print("Just completed writing csv file with %d columns" % len(header))
move()