【问题标题】:Parsing 'user' data from Twitter JSON data using Python使用 Python 从 Twitter JSON 数据中解析“用户”数据
【发布时间】:2014-04-23 20:32:34
【问题描述】:

我正在尝试使用 Python 解析一些 Twitter 数据,以在推文的“用户”字段中提取信息。下面是我的代码,以及我不断收到的错误。我的目标是打印出数据集中每一行的屏幕名称、关注者数量、朋友数量。我确信这对我来说是一个简单的问题,但非常感谢任何帮助。

代码

导入 json 导入系统

def main():


    for line in sys.stdin:
        line = line.strip()

        data = ''

        try:
            data = json.loads(line)
        except ValueError as detail:
           continue

        if not (isinstance(data, dict)):
            ## not a dictionary, skip
            pass
        elif 'delete' in data:
            ## a delete element, skip for now.
            pass
        elif 'user' not in data:
            ## bizarre userless edge case
            pass
        else:
            print "\t".join([
            data['user']['screen_name'],
            data['user']['followers_count'], 
            data['user']['friends_count']
            ])

if __name__ == '__main__':
    main()

错误:

Traceback (most recent call last):
  File "/home/titan/print_info_FAST.py", line 33, in <module>
    main()
File "/home/titan/print_info_FAST.py", line 29, in main
  data['user']['friends_count']
TypeError: sequence item 1: expected string or Unicode, int found    

【问题讨论】:

    标签: python json twitter


    【解决方案1】:

    join 方法需要一个字符串列表,但您的列表包含整数,您需要将整数解析为字符串,试试这个:

    print "\t".join([
    data['user']['screen_name'],
    str(data['user']['followers_count']), 
    str(data['user']['friends_count'])
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多