Python3.5加载json文件,这一行代码总是提示错误,TypeError: the JSON object must be str, not 'bytes'搜索了很多博客,尝试了更改f.encode('utf-8'),照样报错,常规的方法解决不了问题。
有问题的代码:
# read json data
with open(args.recog_json, 'rb') as f:
js = json.load(f)['utts']
后来单独测试json文件,最终改为这样,解决问题:
with open(args.recog_json, 'r',encoding='utf-8') as f:
# read json data
with open(args.recog_json, 'r',encoding='utf-8') as f:
js = json.load(f)['utts']
本文解决了一个常见的Python编程问题,即使用Python3.5读取JSON文件时遇到TypeError: the JSON object must be str, not 'bytes'的错误。通过调整文件打开方式,从'rb'更改为'r'并指定编码为'utf-8',成功解决了问题。
886

被折叠的 条评论
为什么被折叠?



