forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVtoJSON.py
More file actions
24 lines (18 loc) · 648 Bytes
/
CSVtoJSON.py
File metadata and controls
24 lines (18 loc) · 648 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
import csv
import json
def csv_to_json(csv_file, json_file):
# Open the CSV file
with open(csv_file, 'r') as file:
# Read the CSV data
csv_data = csv.DictReader(file)
# Convert CSV to JSON
json_data = json.dumps(list(csv_data), indent=4)
# Write the JSON data to a file
with open(json_file, 'w') as json_file:
json_file.write(json_data)
# Specify the CSV and JSON file paths
csv_file = 'input.csv'
json_file = 'output.json'
# Convert CSV to JSON
csv_to_json(csv_file, json_file)
print(f"CSV file '{csv_file}' has been converted to JSON file '{json_file}'.")