File size: 843 Bytes
9963a74 |
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 |
# Import the csv and json modules
import csv
import json
# Define the input and output file names
csv_file = "merged.csv"
json_file = "output.json"
# Open the csv file in read mode
with open(csv_file, "r", encoding="utf-8") as infile:
# Create a csv reader object
reader = csv.reader(infile)
# Get the header row
header = next(reader)
# Create an empty list to store the data
data = []
# Loop through the rest of the rows
for row in reader:
# Create a dictionary for each row using the header as keys
record = dict(zip(header, row))
# Append the dictionary to the data list
data.append(record)
# Open the json file in write mode
with open(json_file, "w", encoding="utf-8") as outfile:
# Dump the data list as json to the output file
json.dump(data, outfile, indent=4)
|