以文本格式,读取json文件时,出现如下类型错误:
NameError: name 'false' is not defined
NameError: name 'null' is not defined
原因是因为直接以为本流读取json,导致'false'
,'null'
被识别为了为定义的python变量。
解决方案:
很简单,改为用json加载。
例如line
是一个以文本格式直接从json文件读取出来的str
:
line='{"truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null}'
# data = eval(line) ## 不要用eval,将str直接转为python字典
data = json.loads(line) ## 而是改为用json.loads(),将str正确地解析为python字典