我将访问实时twitter tweets的Python脚本的输出通过管道传输到文件output.txt,使用:
$python scriptTweet.py > output.txt
最初,脚本返回的输出是一个写入文本文件的字典。
现在我想使用output.txt文件来访问其中存储的tweets。但是,当我使用以下代码使用json.loads()将output.txt中的文本解析为python字典时:
tweetfile = open("output.txt")
pyresponse = json.loads('tweetfile.read()')
print type(pyresponse)
弹出此错误:
pyresponse = json.loads('tweetfile.read()')
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
如何将output.txt文件的内容再次转换为字典?
'tweetfile.read()'
是一个字符串。要调用此函数:或者直接用
json.load
来读,让json
read
在tweetfile
本身上:相关问题 更多 >
编程相关推荐