【发布时间】:2014-07-02 22:26:26
【问题描述】:
我正在尝试使用 Python 读取 Twitter 流。
我的文件中似乎正确的行如下所示:
{"delete":{"status":{"id":471622360253345792,"user_id":2513833684,"id_str":"471622360253345792","user_id_str":"2513833684"}}}
当我使用 readline 将此行读入内存并对其调用 json.loads() 时,我收到以下错误:
No JSON object could be decoded
我想我必须在调用 json.loads() 之前以某种方式转换该行?
一些注意事项:
- 如果我将文件中的字符串粘贴到 IPython 中并在其上调用 json.loads(),那么一切正常。
-
当我在 IPython 中打印该行时,它会在前面添加一个奇怪的字符,并在其余字符之间放置空格。前几个字符看起来像:
�{"d e l e t e" : {" s t a t u s
-
如果我在 IPython 中显示字符串而不调用 print,前几个字符是:
\xff\xfe{\x00"\x00d\x00e\x00l\x00e\x00t\x00e\x00"\x00:\x00{\x00"\x00s\x00t\x00a\x00t\x00u\x00s\x00" \x00
我不知道如何解决这个问题。
编辑:根据要求,读取推特流的代码在这里:
https://github.com/uwescience/datasci_course_materials/blob/master/assignment1/twitterstream.py
【问题讨论】:
-
我们能看到你的实际代码吗?特别是阅读的部分。
-
FF FE 标头显示字节流被编码为 UTF-8。见:stackoverflow.com/questions/2223882/…