简体   繁体   English

格式化和解码Twitter流JSON输出

[英]Formatting and Decoding Twitter Stream JSON Output

I've having trouble formatting and encoding the twitter stream that is being collected with a python script I wrote. 我在格式化和编码使用我编写的python脚本收集的twitter流时遇到麻烦。 The output looks like this: 输出看起来像这样:

{"created_at":"Wed May 07 20:53:05 +0000 2014", "id":464145921098674177, "id_str":"464145921098674177" ... {“ created_at”:“ 2014年5月7日星期三20:53:05 +0000”,“ id”:464145921098674177,“ id_str”:“ 464145921098674177” ...

... and continues with this single entry along one line. ...,并沿一行继续进行此单个条目。 Each line is a single tweet with massive amounts of information structured just the same. 每行都是一条推文,具有大量相同结构的信息。

I've tried simply using python's JSON module to turn the json file into a dict, but it keeps giving me an error - stating that the structure isn't a in JSON serialization. 我已经尝试过简单地使用python的JSON模块将json文件转换为字典,但是它一直给我一个错误-指出该结构不是JSON序列化。

Ultimately, I'd like to feed the JSON output into a table format. 最终,我想将JSON输出输入为表格格式。 I'm trying to get the file into a csv and go from there. 我正在尝试将文件放入csv,然后从那里去。 I'd settle for anything readable at this point. 在这一点上,我会满足于任何可读性。 FYI - I'm trying to stick to Python because it's what I know. 仅供参考-我想坚持使用Python,因为这是我所知道的。

Here's the python code I tried to use: 这是我尝试使用的python代码:

import json 导入json

json_file = open('twitterOutput.json', 'r').readlines() json_file = open('twitterOutput.json','r')。readlines()

j = json.loads(json_file[0]) j = json.loads(json_file [0])

print j 打印j

Which gives me the error: "No JSON object could be decoded". 这给了我错误:“无法解码JSON对象”。 FYI - this is just test code. 仅供参考-这只是测试代码。 I just wanted to try to get one of the lines of the json_file list to work. 我只是想尝试使json_file列表中的一行起作用。

Thanks. 谢谢。

It's hard to tell where you are going wrong without seeing any code, but the following should do it: 在不看到任何代码的情况下很难分辨出哪里出了问题,但是应该执行以下操作:

import json

twitter_output = # string of twitter output
twitter_output_dict = json.loads(twitter_output)

Or, if the output is stored in a file, then: 或者,如果输出存储在文件中,则:

import json

with open('twitter_output.json') as twitter_output_file:
    twitter_output_dict = json.load(twitter_output_file)

As for "trying to get the file into a csv and go from there", you would have to explain how you want to approach this and how you want it to be structured. 至于“试图将文件放入一个csv并从那里去”,您将不得不解释如何实现该目标以及如何对其进行结构化。 As far as I know, the Twitter-returned JSON is a nested structure (as seen on Twitter's docs so displaying it in csv format really depends on how you want to structure it. 据我所知,Twitter返回的JSON是一个嵌套结构(如在Twitter的文档中所见,因此以csv格式显示它实际上取决于您要如何构造它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM