-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeJson.py
More file actions
36 lines (26 loc) · 1002 Bytes
/
makeJson.py
File metadata and controls
36 lines (26 loc) · 1002 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
# Credit: https://www.geeksforgeeks.org/convert-csv-to-json-using-python/
import csv
import json
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = []
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
data.append(rows)
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
# Driver Code
# Decide the two file paths according to your
# computer system
csvFilePath = r'./data/twosentencehorror_subs.csv'
jsonFilePath = r'./data/twosentencehorror_subs.json'
# Call the make_json function
make_json(csvFilePath, jsonFilePath)