CSV to json
Verfasst: Montag 27. November 2017, 10:17
ich möchte eine CSV Datei mittels Python 3 in ein json Format bringen. ich habe bereits folgenden Code:
import csv
import json
# Pfade
CSV_PATH = '.C:User\\Test.csv'
JSON_PATH = 'C:User\\\Test.json'
# Reads the file the same way that you did
csv_file = csv.DictReader(open(CSV_PATH, 'r'))
# Created a list and adds the rows to the list
json_list = []
for row in csv_file:
json_list.append(row)
# Writes the json output to the file
file(JSON_PATH, 'w').write(json.dumps(json_list))
Es wird mir bei der letzten Zeile ein Fehler angezeigt. file(JSON_PATH, 'w').write(json.dumps(json_list)).
name 'file' is not defined.
kann ich nicht direkt wie in der dritten Zeile beim JSON PATH einfach einen Pfad und .json deklarieren?
Oder wo ist der Fehler ?
import csv
import json
# Pfade
CSV_PATH = '.C:User\\Test.csv'
JSON_PATH = 'C:User\\\Test.json'
# Reads the file the same way that you did
csv_file = csv.DictReader(open(CSV_PATH, 'r'))
# Created a list and adds the rows to the list
json_list = []
for row in csv_file:
json_list.append(row)
# Writes the json output to the file
file(JSON_PATH, 'w').write(json.dumps(json_list))
Es wird mir bei der letzten Zeile ein Fehler angezeigt. file(JSON_PATH, 'w').write(json.dumps(json_list)).
name 'file' is not defined.
kann ich nicht direkt wie in der dritten Zeile beim JSON PATH einfach einen Pfad und .json deklarieren?
Oder wo ist der Fehler ?