-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2json.py
43 lines (29 loc) · 1.23 KB
/
csv2json.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
import csv
import json
csvfile = open('data.csv', 'rU')
jsonfile = open('data.json', 'w')
fieldnames = ("State", "StateName", "MinWage", "Yearly", "TakeHome", "Food", "Housing", "Transportation", "CostOfLiving", "MoneyRemaining")
reader = csv.DictReader(csvfile, fieldnames, delimiter=',')
final = {};
for row in reader:
final[row['State']] = row
json.dump(final, jsonfile)
jsonfile.write('\n')
csvfile = open('data-twoparents.csv', 'rU')
jsonfile = open('data-twoparents.json', 'w')
fieldnames = ("State", "StateName", "MinWage", "Yearly", "TakeHome", "Food", "Housing", "Transportation", "CostOfLiving", "MoneyRemaining")
reader = csv.DictReader(csvfile, fieldnames, delimiter=',')
final = {};
for row in reader:
final[row['State']] = row
json.dump(final, jsonfile)
jsonfile.write('\n')
csvfile = open('data-twoparents-expenses.csv', 'rU')
jsonfile = open('data-twoparents-expenses.json', 'w')
fieldnames = ("State", "StateName", "MinWage", "Yearly", "TakeHome", "Food", "Housing", "Transportation", "Clothing", "Internet", "Phone", "CostOfLiving", "MoneyRemaining")
reader = csv.DictReader(csvfile, fieldnames, delimiter=',')
final = {};
for row in reader:
final[row['State']] = row
json.dump(final, jsonfile)
jsonfile.write('\n')