2016-12-05 30 views
0
{u'contributors': None, u'truncated': False, u'text': u'@Kor3aYn @YouTube yeet', u'is_quote_status': False, u'in_reply_to_status_id': 805863281042878464L, u'id': 805864211544965122L, u'favorite_count': 0, u'source': u'<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', u'retweeted': False, u'coordinates': None, u'timestamp_ms': u'1480967974922', u'entities': {u'user_mentions': [{u'id': 4249141216L, u'indices': [0, 8], u'id_str': u'4249141216', u'screen_name': u'Kor3aYn', u'name': u'YOUTUBE: Kor3aYn\U0001f1f0\U0001f1f7'}, {u'id': 10228272, u'indices': [9, 17], u'id_str': u'10228272', u'screen_name': u'YouTube', u'name': u'YouTube'}], u'symbols': [], u'hashtags': [], u'urls': []}, u'in_reply_to_screen_name': u'Kor3aYn', u'id_str': u'805864211544965122', u'display_text_range': [18, 22], u'retweet_count': 0, u'in_reply_to_user_id': 4249141216L, u'favorited': False, u'user': {u'follow_request_sent': None, u'profile_use_background_image': False, u'default_profile_image': False, u'id': 4858458939L, u'verified': False, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/728320215986114560/f12oxR9J_normal.jpg', u'profile_sidebar_fill_color': u'000000', u'profile_text_color': u'000000', u'followers_count': 148, u'profile_sidebar_border_color': u'000000', u'id_str': u'4858458939', u'profile_background_color': u'000000', u'listed_count': 0, u'profile_background_image_url_https': u'https://abs.twimg.com/images/themes/theme1/bg.png', u'utc_offset': None, u'statuses_count': 76, u'description': None, u'friends_count': 21, u'location': u'Nuk3town', u'profile_link_color': u'ABB8C2', u'profile_image_url': u'http://pbs.twimg.com/profile_images/728320215986114560/f12oxR9J_normal.jpg', u'following': None, u'geo_enabled': False, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/4858458939/1454015440', u'profile_background_image_url': u'http://abs.twimg.com/images/themes/theme1/bg.png', u'name': u'Smye', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 64, u'screen_name': u'i_Smye', u'notifications': None, u'url': None, u'created_at': u'Thu Jan 28 20:59:06 +0000 2016', u'contributors_enabled': False, u'time_zone': None, u'protected': False, u'default_profile': False, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': u'4249141216', u'lang': u'en', u'created_at': u'Mon Dec 05 19:59:34 +0000 2016', u'filter_level': u'low', u'in_reply_to_status_id_str': u'805863281042878464', u'place': None} 

我在存储为解码数组中有以下内容。我试图获得['id_str']这是两件事情; tweetID和用户ID。从字典中提取数据,如果检查

if ScreenName not in decoded['user']['screen_name']: #if your name is not in the json 
    if decoded['id_str'][1] == Twitter_ID: # lets say 4858458939 

这是我和我的理解是不正确的。我基本上试图让我认为存储在第二个'str_id'的推文的作者,并检查它是否对我的Twitter_ID变量。

请帮助我做些什么。 谢谢

+0

这与JSON有什么关系?您提供的数据是本机Python数据结构 - 如果它在分析之前*使用*为JSON,则无法实现;它现在只是普通的Python数据。 –

+0

我已更改标题,谢谢。 – smye

+0

@smye它不是数组(这是numpy数组)。这个结构是Python中的字典。 – rth

回答

0

我看到的最大问题是decoded['id_str']是一个字符串。这意味着decoded['id_str'][0]是一个字符串中的字符。你的意思是decoded['id_str']

如果这不起作用,通常对于这种类型的转换而言,存在类型套管风险。你确定你在比较整数吗?试试这个:

repr(decoded['id_str']) 

如果你看到u'<number>'(你应该根据你提供的数据),那么你没有一个整数,所以比较整数将无法正常工作。这由设计失败。

>>> unicode(2) == 2 
False 

如果是这样的话,你可以投作为比较的一部分:

# Making sure that ScreenName is unicode to match encoding 
if unicode(ScreenName) not in decoded['user']['screen_name']: 
    # Making sure that twitter ID is an integer. 
    if int(decoded['id_str']) == Twitter_ID: 

,如果你不积极,将永远是数字你可能会更好使用unicode(Twitter_ID)

+0

是的,它总是将int与int进行比较。 print(int(decoded ['id_str'] [1])) 我得到的值为0,所以如果检查失败 我相信这是检查推文的所有者的方式。 – smye