Python 读取Twitter JSON结果

Python 读取Twitter JSON结果,python,json,python-3.x,twitter,Python,Json,Python 3.x,Twitter,阅读这些twitter搜索结果的正确方式是什么 {u'contributors': None, u'truncated': False, u'text': u"Google's deep learning project can figure out where any photo was taken, without geotags https://t.co/8URtvHUgjx https://t.co/hTQobCpA4U", u'is_quote_status': False, u'in

阅读这些twitter搜索结果的正确方式是什么

{u'contributors': None, u'truncated': False, u'text': u"Google's deep learning project can figure out where any photo was taken, without geotags https://t.co/8URtvHUgjx https://t.co/hTQobCpA4U", u'is_quote_status': False, u'in_reply_to_status_id': None, u'id': 703129624285286400, u'favorite_count': 198, u'source': u'<a href="http://sproutsocial.com" rel="nofollow">Sprout Social</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': [{u'url': u'https://t.co/8URtvHUgjx', u'indices': [89, 112], u'expanded_url': u'http://www.theverge.com/2016/2/25/11112594/google-new-deep-learning-image-location-planet?utm_campaign=theverge&utm_content=chorus&utm_medium=social&utm_source=twitter', u'display_url': u'theverge.com/2016/2/25/1111\u2026'}], u'media': [{u'source_user_id': 275686563, u'source_status_id_str': u'702916863450345474', u'expanded_url': u'http://twitter.com/verge/status/702916863450345474/photo/1', u'display_url': u'pic.twitter.com/hTQobCpA4U', u'url': u'https://t.co/hTQobCpA4U', u'media_url_https': u'https://pbs.twimg.com/media/CcFDKaHWEAEyUOR.jpg', u'source_user_id_str': u'275686563', u'source_status_id': 702916863450345474, u'id_str': u'702916862934388737', u'sizes': {u'small': {u'h': 383, u'resize': u'fit', u'w': 680}, u'large': {u'h': 675, u'resize': u'fit', u'w': 1200}, u'medium': {u'h': 675, u'resize': u'fit', u'w': 1200}, u'thumb': {u'h': 150, u'resize': u'crop', u'w': 150}}, u'indices': [113, 136], u'type': u'photo', u'id': 702916862934388737, u'media_url': u'http://pbs.twimg.com/media/CcFDKaHWEAEyUOR.jpg'}]}, u'in_reply_to_screen_name': None, u'in_reply_to_user_id': None, u'retweet_count': 232, u'id_str': u'703129624285286400', u'favorited': False, u'user': {u'follow_request_sent': False, u'has_extended_profile': False, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 275686563, u'profile_background_image_url_https': u'https://pbs.twimg.com/profile_background_images/481546505468145664/a59ZFvIP.jpeg', u'verified': True, u'profile_text_color': u'333333', u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/615501837341466624/I4jVBBp-_normal.jpg', u'profile_sidebar_fill_color': u'EFEFEF', u'entities': {u'url': {u'urls': [{u'url': u'http://t.co/W2SFxIXkC4', u'indices': [0, 22], u'expanded_url': u'http://www.theverge.com', u'display_url': u'theverge.com'}]}, u'description': {u'urls': [{u'url': u'https://t.co/W2SFxIXkC4', u'indices': [0, 23], u'expanded_url': u'http://www.theverge.com', u'display_url': u'theverge.com'}]}}, u'followers_count': 1180845, u'profile_sidebar_border_color': u'000000', u'id_str': u'275686563', u'profile_background_color': u'FFFFFF', u'listed_count': 29266, u'is_translation_enabled': True, u'utc_offset': -18000, u'statuses_count': 88374, u'description': u'https://t.co/W2SFxIXkC4 covers the future of technology, science, art, and culture. Snapchat: verge', u'friends_count': 139, u'location': u'New York', u'profile_link_color': u'FA4D2A', u'profile_image_url': u'http://pbs.twimg.com/profile_images/615501837341466624/I4jVBBp-_normal.jpg', u'following': False, u'geo_enabled': True, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/275686563/1433249898', u'profile_background_image_url': u'http://pbs.twimg.com/profile_background_images/481546505468145664/a59ZFvIP.jpeg', u'screen_name': u'verge', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 1217, u'name': u'The Verge', u'notifications': False, u'url': u'http://t.co/W2SFxIXkC4', u'created_at': u'Fri Apr 01 19:54:22 +0000 2011', u'contributors_enabled': False, u'time_zone': u'Eastern Time (US & Canada)', u'protected': False, u'default_profile': False, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'en', u'created_at': u'Fri Feb 26 08:09:00 +0000 2016', u'in_reply_to_status_id_str': None, u'place': None, u'metadata': {u'iso_language_code': u'en', u'result_type': u'popular'}}
TypeError:字符串索引必须是整数,而不是str

尝试以下代码时,我得到ValueError:

with open('../data/full_results.txt', 'r') as fh:
    for line in fh:
        tweet = json.loads(line)
        print(tweet['text'])
ValueError:应为属性名称:第1行第2列(字符1)

但是当我在
Ipython
中为变量分配相同的twitter响应行时

In [2]: tweet = {u'contributors': None, ... u'result_type': u'popular'}}
In [3]: tweet[text]
Out [3]: u"Google's deep learning ...."

它给出了正确的结果。但我不明白为什么?

tweet
是从文件中读取的一行,而不是字典。而且,看起来每一行都不是有效的JSON字符串,而是字典的字符串表示形式。首先要检查/修复的是这些tweet最初是如何以这种格式转储到这个文件中的。您需要在输出文件中使用或包含正确的JSON。然后,要阅读tweet,如果你每行有一条tweet,以下内容应该可以:

import json

with open('../data/full_results.txt', 'r') as fh:
    for line in fh:
        tweet = json.loads(line)
        print(tweet['text'])
如果您有一个转储到JSON的tweet列表:

import json

with open('../data/full_results.txt', 'r') as fh:
    tweets = json.load(fh)
    for tweet in tweets:
        print(tweet['text'])
如果无法更改推文转储到文件中的方式,则可以使用以下内容加载推文:


对于fh中的tweet:
正在对行中的字符进行迭代;您正在以字符串形式读取文件,实际上您没有字典。您提供的字典示例是,它在您的
results.txt
中是如何显示的?此文件每行有一条tweet,总共有多条tweet。我只想提取不同的字段值,如“text”、“favorite_count”等,我尝试了这个方法,但我得到了这个错误:“ValueError:期望属性名:第1行第2列(char 1)”非常感谢!!我爱你:)坚持了3个小时,你救了我!
import json

with open('../data/full_results.txt', 'r') as fh:
    tweets = json.load(fh)
    for tweet in tweets:
        print(tweet['text'])
from ast import literal_eval

with open('../data/full_results.txt', 'r') as fh:
    for line in fh:
        tweet = literal_eval(line)
        print(tweet['text'])