| # 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) | |