-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert_json_to_csv.py
30 lines (27 loc) · 1 KB
/
convert_json_to_csv.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
# -*- coding: UTF-8 -*-
import argparse
import json
import csv
def convert_data(my_json, my_csv):
input_json = open(my_json, "r", encoding="utf-8")
with open(my_csv, "w", encoding="utf-8") as output_csv:
csv_writer = csv.writer(output_csv)
flag = 0
for line in input_json.readlines():
dic = json.loads(line)
# writing headline in the beginning
if flag == 0:
csv_writer.writerow(dic)
flag = 1
csv_writer.writerow(dic.values())
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--json',
default='reviews_Clothing_Shoes_and_Jewelry_5.json',
help='XXXX.json, XXXX is the file name')
parser.add_argument('--csv',
default='output.csv',
help='XXXX.csv, XXXX is the file name')
args = parser.parse_args()
convert_data(args.json, args.csv)
print("Convert Success")