正在阅读Twitter JSON resu

2024-04-27 04:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

阅读这些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'}}

我尝试了以下代码,但它总是抛出错误:

^{pr2}$

TypeError: string indices must be integers, not str

在尝试下面的代码时,我得到ValueError:

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

ValueError: Expecting property name: line 1 column 2 (char 1)

但是当我把同一个twitter响应行赋给Ipython中的一个变量时

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

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


Tags: httpsimagecomnoneidfalsehttpurl
1条回答
网友
1楼 · 发布于 2024-04-27 04:41:09

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

import json

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

如果有一个的tweet列表转储到JSON:

^{pr2}$

如果无法更改将tweet转储到文件中的方式,则可以使用^{}加载tweets:

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'])

相关问题 更多 >