-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsontocsv.py
47 lines (39 loc) · 1.25 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
import json
import csv
#yelp_data = open("yelp_academic_dataset_review.json")
with open("shortreviews.json") as f:
data = json.loads(f.read())
#print(data[0]['text'])
data = json.dumps(data)
yelp_parsed = json.loads(data) #yelp_data??
# open a file for writing
yelp_csv_data = open('/tmp/YelpData.csv', 'w')
# create the csv writer object
csvwriter = csv.writer(yelp_csv_data)
count = 0
with open('YelpData.csv', 'w', newline='') as file2:
res_header = yelp_parsed[0]
header = res_header.keys()
header_trimmed = []
for h in header:
if (h == "stars" or h == "user_id" or h == "text"):
header_trimmed.append(h)
writer = csv.writer(file2)
writer.writerow(header_trimmed)
for res in yelp_parsed:
res_values_trimmed = []
#print(res)
#print(res.keys())
#csvwriter.writerow(header)
#print(res.values())
res_values_trimmed.append(res["user_id"])
res_values_trimmed.append(res["stars"])
res_values_trimmed.append(res["text"])
writer.writerow(res_values_trimmed)
#writer.writerow(res.values())
count += 1
#print(res.values())
#csvwriter.writerow(res.values())
yelp_csv_data.close()
file2.close()
f.close()