Files
FH/WS24_25/PyCharm/pythonProject/P2/cvsToJson.py
2024-09-30 19:27:17 +02:00

30 lines
827 B
Python

import json
def csv_to_json(input_file: str, output_file: str):
data = []
# Step 1: Read the CSV file manually
with open(input_file, 'r') as file:
lines = file.readlines()
# Step 2: Parse the first line to get the headers
headers = lines[0].strip().split(';')
# Step 3: Parse the subsequent lines to get the data
for line in lines[1:]:
values = line.strip().split(';')
# Create a dictionary for each row
row_dict = {headers[i]: values[i] for i in range(len(headers))}
data.append(row_dict)
# Step 4: Save the data to a JSON file
with open(output_file, 'w') as json_file:
json.dump(data, json_file, indent=4)
# Usage
input_csv_file = 'AmesHousing.csv'
output_json_file = 'AmesHousing.json'
csv_to_json(input_csv_file, output_json_file)