以json格式读取和访问twitter响应

时间:2016-07-04 09:32:58

标签: python json

我正在以json的形式阅读Twitter响应并试图阅读它的内容。

我已经应用了json加载,即使它将结果值显示为无效的json。 Twitter响应已存储到.json文件

    with open(filename) as f:
        for line_terminated in f:
            line = line_terminated.rstrip('\n')
            if (len(line.split())) != 0:
                doc = json.loads(line)
                print doc

doc的一个值是:

{u'contributors': None, u'truncated': False, u'text': u'RT @eci_ttip: UK Leave vote deflates hopes for #TTIP trade deal https://fdfdR1hIwdqng1 via @moneycontrolcom', u'is_quote_status': False, u'in_reply_to_status_id': None, u'id': 747348906208460800, u'favorite_count': 0, u'entities': {u'user_mentions': [{u'indices': [3, 12], u'id': 2613612890, u'screen_name': u'eci_ttip', u'name': u'Stop TTIP', u'id_str': u'2613612890'}, {u'indices': [92, 108], u'id': 68927629, u'screen_name': u'moneycontrolcom', u'name': u'moneycontrol', u'id_str': u'68927629'}], u'symbols': [], u'hashtags': [{u'indices': [47, 52], u'text': u'TTIP'}], u'urls': [{u'indices': [64, 87], u'url': u'https://twtR1hIwdqng1', u'expanded_url': u'http://t.in.com/21yS', u'display_url': u't.in.com/21yS'}]}, u'retweeted': False, u'coordinates': None, u'timestamp_ms': u'1467016838968', u'source': u'<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>', u'in_reply_to_screen_name': None, u'id_str': u'747348906208460800', u'retweet_count': 0, u'in_reply_to_user_id': None, u'favorited': False, u'retweeted_status': {u'contributors': None, u'truncated': False, u'text': u'UK Leave vote deflates hopes for #TTIP trade deal https://ttwitter.cme/R1hIwdqng1 via @moneycontrolcom', u'is_quote_status': False, u'in_reply_to_status_id': None, u'id': 747347273579794432, u'favorite_count': 2, u'entities': {u'user_mentions': [{u'indices': [78, 94], u'id': 68927629, u'screen_name': u'moneycontrolcom', u'name': u'moneycontrol', u'id_str': u'68927629'}], u'symbols': [], u'hashtags': [{u'indices': [33, 38], u'text': u'TTIP'}], u'urls': [{u'indices': [50, 73], u'url': u'https://twitter.com', u'expanded_url': u'http://t.in.com/21yS', u'display_url': u't.in.com/21yS'}]}, u'retweeted': False, u'coordinates': None, u'source': u'<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', u'in_reply_to_screen_name': None, u'id_str': u'747347273579794432', u'retweet_count': 5, u'in_reply_to_user_id': None, u'favorited': False, u'user': {u'follow_request_sent': None, u'profile_use_background_image': True, u'contributors_enabled': False, u'id': 2613612890, u'verified': False, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/745615537737207808/decyhsav_normal.jpg', u'profile_sidebar_fill_color': u'DDEEF6', u'profile_text_color': u'333333', u'followers_count': 13887, u'protected': False, u'id_str': u'2613612890', u'default_profile_image': False, u'listed_count': 316, u'utc_offset': 10800, u'statuses_count': 4764, u'description': u"The self-organized European Citizens' Initiative to stop TTIP and CETA which from October 7th 2015 continues campaigning as an European Initiative.", u'friends_count': 1090, u'location': u'European Union', u'profile_link_color': u'0084B4', u'profile_image_url': u'http://pbs.twimg.com/profile_images/745615537737207808/decyhsav_normal.jpg', u'following': None, u'geo_enabled': False, u'profile_background_color': u'C0DEED', u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/2613612890/1466603542', u'profile_background_image_url': u'http://abs.twimg.com/images/themes/theme1/bg.png', u'name': u'Stop TTIP', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 3409, u'screen_name': u'eci_ttip', u'notifications': None, u'url': u'http://stop-ttip.org', u'created_at': u'Wed Jul 09 14:04:30 +0000 2014', u'profile_background_image_url_https': u'https://abs.twimg.com/images/themes/theme1/bg.png', u'time_zone': u'Athens', u'profile_sidebar_border_color': u'C0DEED', u'default_profile': True, 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'Mon Jun 27 08:34:09 +0000 2016', u'filter_level': u'low', u'in_reply_to_status_id_str': None, u'place': None}, u'user': {u'follow_request_sent': None, u'profile_use_background_image': True, u'contributors_enabled': False, u'id': 145999456, u'verified': False, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/747179195361562624/KzWjBe0R_normal.jpg', u'profile_sidebar_fill_color': u'DDEEF6', u'profile_text_color': u'333333', u'followers_count': 1692, u'protected': False, u'id_str': u'145999456', u'default_profile_image': False, u'listed_count': 47, u'utc_offset': 7200, u'statuses_count': 90111, u'description': u'http://noalttip.org/actua El conocimiento es el ant\xeddoto del miedo  #noTTIP', u'friends_count': 2385, u'location': u'a otra cosa mariposa,  espe ', u'profile_link_color': u'0084B4', u'profile_image_url': u'http://pbs.twimg.com/profile_images/747179195361562624/KzWjBe0R_normal.jpg', u'following': None, u'geo_enabled': True, u'profile_background_color': u'C0DEED', u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/145999456/1449143895', u'profile_background_image_url': u'http://pbs.twimg.com/profile_background_images/297105510/Spanishrevolution2-640x420.jpg', u'name': u'Ram\xf3n #noTTIP', u'lang': u'es', u'profile_background_tile': False, u'favourites_count': 11060, u'screen_name': u'klf129', u'notifications': None, u'url': u'http://noalttip.blogspot.com.es/?m=1', u'created_at': u'Thu May 20 11:01:46 +0000 2010', u'profile_background_image_url_https': u'https://pbs.twimg.com/profile_background_images/297105510/Spanishrevolution2-640x420.jpg', u'time_zone': u'Amsterdam', u'profile_sidebar_border_color': u'C0DEED', 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'Mon Jun 27 08:40:38 +0000 2016', u'filter_level': u'low', u'in_reply_to_status_id_str': None, u'place': None}

我无法以dic身份访问。

如果我错了,有人可以纠正。

1 个答案:

答案 0 :(得分:2)

  

我无法以dic身份访问

呃,不,doc是字典,可以使用键访问值:

>>> doc = {u'contributors': None, u'truncated': False}
>>> doc['truncated']
False

但是你有一些包含在列表中的值。要访问这些值,您需要访问父词典的键,在列表值索引列表,然后访问列表中词典的键。像这样:

>>> doc['entities']['user_mentions']
[{'screen_name': 'eci_ttip', 'id_str': '2613612890', 'id': 2613612890, 'indices': [3, 12], 'name': 'Stop TTIP'}, {'screen_name': 'moneycontrolcom', 'id_str': '68927629', 'id': 68927629, 'indices': [92, 108], 'name': 'moneycontrol'}]
>>>
>>> doc['entities']['user_mentions'][0]
{'screen_name': 'eci_ttip', 'id_str': '2613612890', 'id': 2613612890, 'indices': [3, 12], 'name': 'Stop TTIP'}
>>>
>>> doc['entities']['user_mentions'][0]['screen_name']
'eci_ttip'

因此,您应该知道dict中特定值的结构和位置,以便访问它。

我建议您使用pprint打印字典,以便更好地了解整个结构:

from pprint import pprint

pprint(doc)