我用的是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 <module>
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