在Python中读取json文件时出现以下报错:

代码:

 

import json
file = open('E:\\usagov_bitly_data.txt')
data = json.loads(file)

报错:

TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

主要原因:

json.loads()是在内存中转换;json.load()是对应于文件的处理。修改后运行成功。

 

json.load():针对文件,将json格式的字符转换为dict,从文件中读取。

json.loads():针对内存对象,将string转换为dict