2016-12-05 7 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} 

私は以下の配列をデコードされた配列として格納しています。私は2つのことである['id_str']を取得しようとしています。ツイートIDとユーザ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 

これは私が持っているものであり、正しくはないことを理解しています。私は本質的に、2番目の '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']を意味します。

これが機能しない場合、通常、このタイプの変換では、ケースの危険性があります。あなたはintをintと比較していますか?これを試してみてください:

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を常に比較しています。 0の値が得られるので、ifチェックに失敗します これはツイートの所有者を確認する方法であると確信しています。 – smye

関連する問題