-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv2json.py
More file actions
44 lines (30 loc) · 1.04 KB
/
Copy pathcsv2json.py
File metadata and controls
44 lines (30 loc) · 1.04 KB
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
import csv
import json
import argparse
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
#read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
if __name__ == "__main__":
datafile = ""
title = ""
output_file = ""
parser = argparse.ArgumentParser()
parser.add_argument("csvfile")
parser.add_argument("jsonfile")
args = parser.parse_args()
if args.csvfile:
csvFilePath = args.csvfile
if args.jsonfile:
jsonFilePath = args.jsonfile
csv_to_json(csvFilePath, jsonFilePath)