我想遍历标准Twitter流API的输出,如果country_code =='US',则将地名打印到stdout:
import sys
import json
def loc_tweets(tw_file):
for line in tw_file:
tweet = json.loads(line)
if tweet.get("country_code", None) == "US":
print tweet.get("full_name", None)
else:
print "Nope"
def main():
tweet_file = open(sys.argv[1])
loc_tweets(tweet_file)
if __name__ == '__main__':
main()
我的最终目标是对所有tweet的情绪进行评分,然后将美国各州的平均情绪汇总到字典中,但是我必须走起来才能跑步。我尝试将if条件设置为非None:
if tweet.get("country_code", None) != None:
但是“ Nope”是我仍然收到的唯一值。我知道我使用的文件包含有效的country_codes和full_names,因为我手动对文件进行了排序并找到了它们。不确定我做错了什么,我们将不胜感激!
编辑:
带有有效country_code的json示例行:
{"created_at":"Wed Jul 18 16:38:00 +0000 2018","id":1019622325233172480,"id_str":"1019622325233172480","text":"RT @sahluwal: A video of President Barack Obama dancing with his grandmother to start your day.\n\nhttps:\/\/t.co\/0bI6a742Jd","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":50801833,"id_str":"50801833","name":"Crimea River","screen_name":"HooplaHappens","location":"Way Out West","url":"http:\/\/hooplahappens.blogspot.com\/","description":"Journalista-Oostoopka","translator_type":"none","protected":false,"verified":false,"followers_count":2879,"friends_count":3224,"listed_count":119,"favourites_count":64181,"statuses_count":49724,"created_at":"Thu Jun 25 22:56:05 +0000 2009","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"352726","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme5\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme5\/bg.gif","profile_background_tile":false,"profile_link_color":"592003","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"688A24","profile_text_color":"080808","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/439979120118603777\/oIQkFKeX_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/439979120118603777\/oIQkFKeX_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/50801833\/1401678817","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Wed Jul 18 12:56:04 +0000 2018","id":1019566473092579329,"id_str":"1019566473092579329","text":"A video of President Barack Obama dancing with his grandmother to start your day.\n\nhttps:\/\/t.co\/0bI6a742Jd","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":2432416417,"id_str":"2432416417","name":"Simar","screen_name":"sahluwal","location":"Los Angeles, California","url":null,"description":"[ USC alum\u270c\ud83c\udffd] [ Activist ] [ \ud83d\udc7b: Sahluwal03 ]","translator_type":"none","protected":false,"verified":false,"followers_count":26535,"friends_count":261,"listed_count":552,"favourites_count":27035,"statuses_count":9883,"created_at":"Mon Apr 07 18:07:46 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/910986479698026496\/lXlV1q1N_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/910986479698026496\/lXlV1q1N_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2432416417\/1513750709","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":{"id":"3b77caf94bfc81fe","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/3b77caf94bfc81fe.json","place_type":"city","name":"Los Angeles","full_name":"Los Angeles, CA","country_code":"US","country":"United States","bounding_box":{"type":"Polygon","coordinates":[[[-118.668404,33.704538],[-118.668404,34.337041],[-118.155409,34.337041],[-118.155409,33.704538]]]},"attributes":{}},"contributors":null,"is_quote_status":false,"quote_count":54,"reply_count":78,"retweet_count":374,"favorite_count":1317,"entities":{"hashtags":[],"urls":[],"user_mentions":[],"symbols":[],"media":[{"id":1018967307010363404,"id_str":"1018967307010363404","indices":[83,106],"additional_media_info":{"title":"Obama Dancing In Kenya Is What You Need To See Today","description":"Obama dancing in Kenya is the perfect antidote to today","embeddable":true,"monetizable":false},"media_url":"http:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","media_url_https":"https:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","url":"https:\/\/t.co\/0bI6a742Jd","display_url":"pic.twitter.com\/0bI6a742Jd","expanded_url":"https:\/\/twitter.com\/nowthisnews\/status\/1019017086738321408\/video\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":680,"resize":"fit"},"medium":{"w":720,"h":720,"resize":"fit"},"large":{"w":720,"h":720,"resize":"fit"}},"source_status_id":1019017086738321408,"source_status_id_str":"1019017086738321408","source_user_id":701725963,"source_user_id_str":"701725963"}]},"extended_entities":{"media":[{"id":1018967307010363404,"id_str":"1018967307010363404","indices":[83,106],"additional_media_info":{"title":"Obama Dancing In Kenya Is What You Need To See Today","description":"Obama dancing in Kenya is the perfect antidote to today","embeddable":true,"monetizable":false},"media_url":"http:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","media_url_https":"https:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","url":"https:\/\/t.co\/0bI6a742Jd","display_url":"pic.twitter.com\/0bI6a742Jd","expanded_url":"https:\/\/twitter.com\/nowthisnews\/status\/1019017086738321408\/video\/1","type":"video","video_info":{"aspect_ratio":[1,1],"duration_millis":67301,"variants":[{"bitrate":832000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/vid\/480x480\/heybLft1B4hZKx0R.mp4?tag=3"},{"content_type":"application\/x-mpegURL","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/pl\/FZ3HhEcJ-n0Ymg7W.m3u8?tag=3"},{"bitrate":1280000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/vid\/720x720\/Tsc4McEaTumEWMaY.mp4?tag=3"},{"bitrate":288000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/vid\/240x240\/k613GtxxYDy5Xq2e.mp4?tag=3"}]},"sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":680,"resize":"fit"},"medium":{"w":720,"h":720,"resize":"fit"},"large":{"w":720,"h":720,"resize":"fit"}},"source_status_id":1019017086738321408,"source_status_id_str":"1019017086738321408","source_user_id":701725963,"source_user_id_str":"701725963"}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"sahluwal","name":"Simar","id":2432416417,"id_str":"2432416417","indices":[3,12]}],"symbols":[],"media":[{"id":1018967307010363404,"id_str":"1018967307010363404","indices":[97,120],"additional_media_info":{"title":"Obama Dancing In Kenya Is What You Need To See Today","description":"Obama dancing in Kenya is the perfect antidote to today","embeddable":true,"monetizable":false},"media_url":"http:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","media_url_https":"https:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","url":"https:\/\/t.co\/0bI6a742Jd","display_url":"pic.twitter.com\/0bI6a742Jd","expanded_url":"https:\/\/twitter.com\/nowthisnews\/status\/1019017086738321408\/video\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":680,"resize":"fit"},"medium":{"w":720,"h":720,"resize":"fit"},"large":{"w":720,"h":720,"resize":"fit"}},"source_status_id":1019017086738321408,"source_status_id_str":"1019017086738321408","source_user_id":701725963,"source_user_id_str":"701725963"}]},"extended_entities":{"media":[{"id":1018967307010363404,"id_str":"1018967307010363404","indices":[97,120],"additional_media_info":{"title":"Obama Dancing In Kenya Is What You Need To See Today","description":"Obama dancing in Kenya is the perfect antidote to today","embeddable":true,"monetizable":false},"media_url":"http:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","media_url_https":"https:\/\/pbs.twimg.com\/amplify_video_thumb\/1018967307010363404\/img\/1KAp8NvrJggTZtY5.jpg","url":"https:\/\/t.co\/0bI6a742Jd","display_url":"pic.twitter.com\/0bI6a742Jd","expanded_url":"https:\/\/twitter.com\/nowthisnews\/status\/1019017086738321408\/video\/1","type":"video","video_info":{"aspect_ratio":[1,1],"duration_millis":67301,"variants":[{"bitrate":832000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/vid\/480x480\/heybLft1B4hZKx0R.mp4?tag=3"},{"content_type":"application\/x-mpegURL","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/pl\/FZ3HhEcJ-n0Ymg7W.m3u8?tag=3"},{"bitrate":1280000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/vid\/720x720\/Tsc4McEaTumEWMaY.mp4?tag=3"},{"bitrate":288000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/amplify_video\/1018967307010363404\/vid\/240x240\/k613GtxxYDy5Xq2e.mp4?tag=3"}]},"sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":680,"resize":"fit"},"medium":{"w":720,"h":720,"resize":"fit"},"large":{"w":720,"h":720,"resize":"fit"}},"source_status_id":1019017086738321408,"source_status_id_str":"1019017086738321408","source_user_id":701725963,"source_user_id_str":"701725963"}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1531931880659"}