TypeError: the JSON object must be str, not 'TextIOWrapper'

网友投稿 838 2022-10-01

TypeError: the jsON object must be str, not 'TextIOWrapper'

TypeError: the JSON object must be str, not 'TextIOWrapper'

我用的是python3,我的加载代码是:

# 读取数据 with open('dict_author_id.json', 'r') as f: data = json.loads(f) print(data["R. Lauterbach"])

然后报错了,错误信息为:

Traceback (most recent call last): File "data_preprocessing.py", line 111, in process() File "data_preprocessing.py", line 105, in process dp.load_json() File "data_preprocessing.py", line 41, in load_json data = json.loads(f) File "/usr/lib/python3.5/json/__init__.py", line 312, in loads s.__class__.__name__))TypeError: the JSON object must be str, not 'TextIOWrapper'

然后查资料,把data = json.loads(f)换成:

data = json.load(f)

就行了.

理由是load是从文件里面load,loads是从str里面load

参考文献

[1].Python - JSON Load from file not working.https://stackoverflow.com/questions/26072148/python-json-load-from-file-not-working

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot整合minio实现文件服务的教程详解
下一篇:小程序商城与淘宝网有什么不同?(小程序商城与淘宝网有什么不同吗)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~