forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_to_json.py
More file actions
42 lines (29 loc) · 857 Bytes
/
csv_to_json.py
File metadata and controls
42 lines (29 loc) · 857 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
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/env python3
import csv
import json
# Put where your CSV File is
CSV_FILE = ''
# Put where you want your JSON file
JSON_FILE = ''
data = []
with open(CSV_FILE, 'r') as f:
csv_data = csv.reader(f)
count = 0
is_header_line = True
headers = []
# Fill up headers with the strings from the header line
for row in csv_data:
if is_header_line:
is_header_line = False
for header in row:
headers.append(header)
continue
datum = {}
for i in range(len(row)):
cell = row[i]
header = headers[i]
datum[header] = cell
data.append(datum)
json_string = json.dumps(data, indent = 4)
with open(JSON_FILE,'w') as f:
f.write(json_string)