-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathcsv_to_json.py
More file actions
32 lines (23 loc) · 755 Bytes
/
csv_to_json.py
File metadata and controls
32 lines (23 loc) · 755 Bytes
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
import csv
import json
file_name = input("Provide the CSV filename without extension>> ")
try:
with open(file_name+'.csv') as f:
reader = csv.reader(f, delimiter=',')
titles = []
temp_data = {}
for heading in reader:
titles = heading
break
i = 1
for row in reader:
current_row = "row{}".format(i)
temp_data['{}'.format(current_row)] = {}
for col in range(len(titles)):
temp_data[current_row][titles[col]] = row[col]
i+=1
with open(file_name+'.json', 'w') as f_j:
json.dump(temp_data, f_j, indent=4)
except:
print("Please provide correct filename")
print("File converted successfully :)\n")