why isn't my parsing from JSON working in python -
with open('tweets.json') json_data: data = json.load(json_data) print(data['text'])
i want extract specific data/values keep getting error:
print(data['text']) typeerror: string indices must integers
i beginner python , trying learn using twitter api
this json:
"{\"created_at\":\"wed feb 03 03:02:04 +0000 2016\",\"id\":694717462621884416,\"id_str\":\"694717462621884416\",\"text\":\"finallyy @taylorcaniff happy bday bae, love soooo much, keep smiling, i'm proud of you've done\\u2661 https:\\/\\/t.co\\/uwjeasxsa3\",\"source\":\"\\u003ca href=\\\"http:\\/\\/twitter.com\\/download\\/android\\\" rel=\\\"nofollow\\\"\\u003etwitter android\\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\":1364125758,\"id_str\":\"1364125758\",\"name\":\"c o l l n e r\",\"screen_name\":\"hoodspizzaxjcat\",\"location\":\"2\\/5 uj | vamps dm\",\"url\":null,\"description\":\"\\u25a8issa liked x2 & follow\\u25a8brent follow, liked x4 &dm\\u25a8chris liked x3 , follows\\u25a8taylor, kizzy, jacob, caspar, king b. & momma collins follow\\u25a8trevor liked \\u25a8\",\"protected\":false,\"verified\":false,\"followers_count\":12136,\"friends_count\":13282,\"listed_count\":20,\"favourites_count\":29245,\"statuses_count\":46864,\"created_at\":\"fri apr 19 10:59:10 +0000 2013\",\"utc_offset\":-10800,\"time_zone\":\"buenos aires\",\"geo_enabled\":true,\"lang\":\"es\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"09ed92\",\"profile_background_image_url\":\"http:\\/\\/pbs.twimg.com\\/profile_background_images\\/506799872326893569\\/vdahwdtj.jpeg\",\"profile_background_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_background_images\\/506799872326893569\\/vdahwdtj.jpeg\",\"profile_background_tile\":true,\"profile_link_color\":\"4cc74c\",\"profile_sidebar_border_color\":\"ffffff\",\"profile_sidebar_fill_color\":\"ddeef6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"profile_image_url\":\"http:\\/\\/pbs.twimg.com\\/profile_images\\/688994368057921536\\/iky-2uyn_normal.jpg\",\"profile_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_images\\/688994368057921536\\/iky-2uyn_normal.jpg\",\"profile_banner_url\":\"https:\\/\\/pbs.twimg.com\\/profile_banners\\/1364125758\\/1450566712\",\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"is_quote_status\":false,\"retweet_count\":0,\"favorite_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[{\"screen_name\":\"taylorcaniff\",\"name\":\"taylor caniff\",\"id\":1396698397,\"id_str\":\"1396698397\",\"indices\":[9,22]}],\"symbols\":[],\"media\":[{\"id\":694717457911693312,\"id_str\":\"694717457911693312\",\"indices\":[116,139],\"media_url\":\"http:\\/\\/pbs.twimg.com\\/media\\/caqh2oiwwaa6g_c.jpg\",\"media_url_https\":\"https:\\/\\/pbs.twimg.com\\/media\\/caqh2oiwwaa6g_c.jpg\",\"url\":\"https:\\/\\/t.co\\/uwjeasxsa3\",\"display_url\":\"pic.twitter.com\\/uwjeasxsa3\",\"expanded_url\":\"http:\\/\\/twitter.com\\/hoodspizzaxjcat\\/status\\/694717462621884416\\/photo\\/1\",\"type\":\"photo\",\"sizes\":{\"large\":{\"w\":480,\"h\":800,\"resize\":\"fit\"},\"thumb\":{\"w\":150,\"h\":150,\"resize\":\"crop\"},\"small\":{\"w\":340,\"h\":566,\"resize\":\"fit\"},\"medium\":{\"w\":480,\"h\":800,\"resize\":\"fit\"}}}]},\"extended_entities\":{\"media\":[{\"id\":694717457911693312,\"id_str\":\"694717457911693312\",\"indices\":[116,139],\"media_url\":\"http:\\/\\/pbs.twimg.com\\/media\\/caqh2oiwwaa6g_c.jpg\",\"media_url_https\":\"https:\\/\\/pbs.twimg.com\\/media\\/caqh2oiwwaa6g_c.jpg\",\"url\":\"https:\\/\\/t.co\\/uwjeasxsa3\",\"display_url\":\"pic.twitter.com\\/uwjeasxsa3\",\"expanded_url\":\"http:\\/\\/twitter.com\\/hoodspizzaxjcat\\/status\\/694717462621884416\\/photo\\/1\",\"type\":\"photo\",\"sizes\":{\"large\":{\"w\":480,\"h\":800,\"resize\":\"fit\"},\"thumb\":{\"w\":150,\"h\":150,\"resize\":\"crop\"},\"small\":{\"w\":340,\"h\":566,\"resize\":\"fit\"},\"medium\":{\"w\":480,\"h\":800,\"resize\":\"fit\"}}}]},\"favorited\":false,\"retweeted\":false,\"possibly_sensitive\":false,\"filter_level\":\"low\",\"lang\":\"en\",\"timestamp_ms\":\"1454468524972\"}\r\n"
data in text format. (not in json). first have convert in json.
>>> json.loads(data)["text"] u"finallyy @taylorcaniff happy bday bae, love soooo much, keep smiling, i'm proud of you've done\u2661 https://t.co/uwjeasxsa3"
Comments
Post a Comment