【发布时间】:2018-06-27 06:59:48
【问题描述】:
我有多个 Json 文件,我想连接/合并并制作一个 Json 文件。 下面的代码给我一个错误
def merge_JsonFiles(*filename):
result = []
for f1 in filename:
with open(f1, 'rb') as infile:
result.append(json.load(infile))
with open('Mergedjson.json', 'wb') as output_file:
json.dump(result, output_file)
# in this next line of code, I want to load that Merged Json files
#so that I can parse it to proper format
with open('Mergedjson.json', 'rU') as f:
d = json.load(f)
以下是我的输入 json 文件的代码
if __name__ == '__main__':
allFiles = []
while(True):
inputExtraFiles = input('Enter your other file names. To Ignore this, Press Enter!: ')
if inputExtraFiles =='':
break
else:
allFiles.append(inputExtraFiles)
merge_JsonFiles(allFiles)
但它给我一个错误
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
另外,我想确保如果它只从控制台获取一个输入文件,则 json 的合并不应该引发错误。
任何帮助,为什么会抛出错误?
更新
结果它返回给我一个空的 Mergedjson 文件。我有有效的 json 格式
【问题讨论】:
-
你能打印堆栈跟踪吗
-
你应该考虑使用pypi.org/project/jsonmerge
标签: python json python-3.x