p2
This commit is contained in:
29
WS24_25/PyCharm/pythonProject/P2/cvsToJson.py
Normal file
29
WS24_25/PyCharm/pythonProject/P2/cvsToJson.py
Normal file
@@ -0,0 +1,29 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user