-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2json.py
More file actions
45 lines (38 loc) · 1.63 KB
/
csv2json.py
File metadata and controls
45 lines (38 loc) · 1.63 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Kleines Python Script um eine CSV einzulesen, Felder umzubenennen, neu zusammenzusetzen und
# als JSON File neu zu schreiben
# copyright: Markus Smieja
# 07.02.2023, v0.2
import csv
import json
csvFilePath = r'VMware Kochbuch Library.csv'
jsonFilePath = r'VMware Kochbuch Library.json'
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
#read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
row['@context'] = 'https://schema.org'
row['@type'] = 'Recipe'
del row['E-Mail']
row['author'] = row['Vorname'] + " " + row['Name']
del row['Vorname']
del row['Name']
row['cookTime'] = row.pop('Zubereitungsdauer')
row['description'] = row.pop('Geschichte des Rezepts')
row['image'] = row.pop('Rezeptbilderpfad')
row['name'] = row.pop('Name des Rezepts')
del row ['Kategorie']
row['recipeIngredient'] = row['Zutatenliste'].split(',')
del row['Zutatenliste']
del row['Autorbilderpfad']
row['recipeInstructions'] = row.pop('Zubereitung')
#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
csv_to_json(csvFilePath, jsonFilePath)