-
Notifications
You must be signed in to change notification settings - Fork 26
/
csv_to_json.py
executable file
·31 lines (28 loc) · 977 Bytes
/
csv_to_json.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
#! /usr/bin/env python3
import os
import csv
import json
print("Scanning CSV directory...")
csvFileList = os.listdir("./csv")
print("Generating JSON from CSV...")
for csvFile in csvFileList:
with open("./csv/"+csvFile) as infile:
reader = csv.DictReader(infile)
name = os.path.basename(csvFile)
name = os.path.splitext(name)[0]
jsonFile = dict()
for row in reader:
partDict = dict()
for item in row:
if item == "size":
continue
# turn number strings to floats if possible
try:
partDict[item] = float(row[item])
except ValueError:
partDict[item] = row[item]
jsonFile[row["size"]] = partDict
outFile = open("./json/"+name+".json", 'w')
json.dump(jsonFile,outFile, indent=2, sort_keys=True)
outFile.close()
print("CSV to JSON conversion finished!")