【发布时间】:2018-03-03 17:13:51
【问题描述】:
目标:将 JSON 文件合并为一个大文件
背景:我使用的代码取自这里Issue with merging multiple JSON files in Python
import json
import glob
result = []
for f in glob.glob("/Users/EER/Desktop/JSON_Combo/*.json"):
with open(f, "rb") as infile:
result.append(json.load(infile))
with open("merged_file.json", "wb") as outfile:
json.dump(result, outfile)
但是,我收到以下错误:
JSONDecodeError: Extra data: line 2 column 1 (char 5733)
我检查了Python json.loads shows ValueError: Extra data、JSONDecodeError: Extra data: line 1 column 228 (char 227) 和ValueError: Extra Data error when importing json file using python,但它们有点不同。错误的一个潜在原因似乎是我的 .json 文件是一个字符串列表,但我不确定
问题:关于如何解决此错误的任何想法?
【问题讨论】:
-
听起来您的文件之一不是有效的 JSON。我建议输入
try ... except...并在except块中打印文件名,看看哪个是坏的。