&引用;TypeError:JSON对象必须是str,而不是';字节'&引用;在Python 3.5中读取JSON
我想运行以下代码,首先是为Python2.7编写的,现在我想为python3.5运行这些代码。 以下是我的代码:&引用;TypeError:JSON对象必须是str,而不是';字节'&引用;在Python 3.5中读取JSON,python,json,python-2.7,python-3.5,Python,Json,Python 2.7,Python 3.5,我想运行以下代码,首先是为Python2.7编写的,现在我想为python3.5运行这些代码。 以下是我的代码: import json import glob read_files = glob.glob("*.json") output_list = [] for f in read_files: with open(f, "r") as infile: output_list.append(json.load(infile)) with open("merged
import json
import glob
read_files = glob.glob("*.json")
output_list = []
for f in read_files:
with open(f, "r") as infile:
output_list.append(json.load(infile))
with open("merged_file.json", "wb") as outfile:
json.dump(output_list, outfile)
但它不断给出以下错误:
raise JSONDecodeError("Expecting value", s, err.value) from
None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
json.load
和json.load
之间有什么区别?我如何确定何时使用哪个呢?将open(f,“rb”)
更改为open(f,“r”)
。rb标志用于读取字节。在打开的(“rb”
->“r”
)中删除二进制模式。我不知道你为什么要把它放在那里。你可以把一个文件读成字符串而不是二进制模式。当我改变它时,它后面跟着一个错误:raise jsondecoderError(“期望值”,s,err.value)from None json.decoder.jsondecoderError:期望值:第1行第1列(char 0)使用json.loads加载字符串