【发布时间】:2019-07-02 04:04:34
【问题描述】:
我正在尝试将一个非常大的 .json 文件转换为一个 .csv 文件。这是我一直在使用的json 文件的示例。 我将直接从期刊出版商处获得相同格式的文件。
这样做的主要目的是从 .json 文件中提取所有组件并将信息放入我们的数据库中。
下面是我试过的代码。
import csv, json, sys
if sys.argv[1] is not None and sys.argv[2] is not None:
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open(fileInput, encoding="utf8") #open json file
outputFile = open(fileOutput, 'w') #load csv file
data = json.load(inputFile) #load json content
inputFile.close() #close the input file
output = csv.writer(outputFile) #create a csv.write
output.writerow(data[0].keys()) # header row
for row in data:
output.writerow(row.values()) #values row
我收到此错误:
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 542)
【问题讨论】:
-
你能确定你的json是有效的吗?
-
@MosheRabaev 实际上发布者发送 .jsonl 文件并且该文件被转换为 .json。转换后的文件是我用于 .csv 转换的文件
-
@MosheRabaev 它不是有效的 json。我在下面的回答深入探讨了为什么它不是有效的 json。
标签: json python-3.x csv