-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvtojson.py
More file actions
39 lines (32 loc) · 1.11 KB
/
csvtojson.py
File metadata and controls
39 lines (32 loc) · 1.11 KB
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
import csv
import json
import sys
def csv_to_json(csv_file_path, json_file_path):
# Set a larger field size limit
maxInt = sys.maxsize
decrement = True
while decrement:
decrement = False
try:
csv.field_size_limit(maxInt)
except OverflowError:
maxInt = int(maxInt/10)
decrement = True
# Open the CSV file
with open(csv_file_path, mode='r') as csv_file:
# Read the CSV file into a dictionary
csv_reader = csv.DictReader(csv_file)
# Create a list to hold all rows
data = []
# Iterate over each row in the CSV
for row in csv_reader:
# Add the row to the list
data.append(row)
# Write the data to a JSON file
with open(json_file_path, mode='w') as json_file:
# Write the data as JSON
json.dump(data, json_file, indent=4)
# Example usage:
csv_file_path = './final_output.csv' # Replace with your CSV file path
json_file_path = './final_output.json' # Replace with desired JSON file path
csv_to_json(csv_file_path, json_file_path)