【发布时间】:2020-07-25 02:48:36
【问题描述】:
我正在尝试将多个 .json 合并到一个 json 文件中
代码:
import glob, json
read_files = glob.glob("*.json")
output_list = []
for f in read_files:
with open(f, "rb") as infile:
output_list.append(json.load(infile))
all_items = []
for json_file in output_list:
all_items += json_file['value']
textfile_merged = open('merged.json', 'w')
json.dump({ "value": all_items }, textfile_merged)
textfile_merged.close()
我收到以下错误
> Traceback (most recent call last): File "D:\JSON\merge.py", line 7,
> in <module>
> output_list.append(json.load(infile)) File "C:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 293, in load
> return loads(fp.read(), File "C:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 357, in loads
> return _default_decoder.decode(s) File "C:\Program Files (x86)\Python38-32\lib\json\decoder.py", line 337, in decode
> obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Program Files (x86)\Python38-32\lib\json\decoder.py", line 355, in
> raw_decode
> raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
> 0)
【问题讨论】:
-
其中一个文件不是有效的 JSON。