将JSON(tweepy)中的数据解析为熊猫数据框

问题描述 投票:0回答:1

第一次张贴在这里。

我已经从Tweepy流了tweets,现在有一个txt文件,其中包含这样的示例tweet(为下面的大量文本表示歉意)。我曾尝试在此处和文档中查找,但不确定如何将所有这些解析为pandas数据框?

{"created_at":"Thu Nov 21 05:52:47 +0000 2019","id":1197392391092736000,"id_str":"1197392391092736000","text":"RT @Reuters: Google to stop giving advertisers the ability to target election ads using data such as public voter records and general polit\u2026","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for 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":23951203,"id_str":"23951203","name":"Sara Jane M\u00e4ki","screen_name":"SaraJaneMaki","location":"Massachusetts, USA","url":null,"description":null,"translator_type":"none","protected":false,"verified":false,"followers_count":3831,"friends_count":3393,"listed_count":49,"favourites_count":541561,"statuses_count":104665,"created_at":"Thu Mar 12 14:04:18 +0000 2009","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":null,"contributors_enabled":false,"is_translator":false,"profile_background_color":"B2DFDA","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme13\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme13\/bg.gif","profile_background_tile":false,"profile_link_color":"93A644","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"FFFFFF","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1196268047994118144\/UDJp5zxL_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1196268047994118144\/UDJp5zxL_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/23951203\/1511316980","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":"Thu Nov 21 05:51:03 +0000 2019","id":1197391954818019328,"id_str":"1197391954818019328","text":"Google to stop giving advertisers the ability to target election ads using data such as public voter records and ge\u2026 https:\/\/t.co\/J1Dt70ARvG","display_text_range":[0,140],"source":"\u003ca href=\"http:\/\/www.socialflow.com\" rel=\"nofollow\"\u003eSocialFlow\u003c\/a\u003e","truncated":true,"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":1652541,"id_str":"1652541","name":"Reuters","screen_name":"Reuters","location":"Around the world","url":"http:\/\/www.reuters.com","description":"Top and breaking news, pictures and videos from Reuters. For more breaking business news, follow @ReutersBiz.","translator_type":"regular","protected":false,"verified":true,"followers_count":20900582,"friends_count":1105,"listed_count":119515,"favourites_count":756,"statuses_count":462380,"created_at":"Tue Mar 20 17:46:05 +0000 2007","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":null,"contributors_enabled":false,"is_translator":false,"profile_background_color":"444444","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":"006F97","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"F5F5F5","profile_text_color":"000000","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1194751949821939712\/3VBu4_Sa_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1194751949821939712\/3VBu4_Sa_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1652541\/1573687397","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,"extended_tweet":{"full_text":"Google to stop giving advertisers the ability to target election ads using data such as public voter records and general political affiliations https:\/\/t.co\/8Iip7bTz7O by @eculliford https:\/\/t.co\/O7i5uw9fFU","display_text_range":[0,182],"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/8Iip7bTz7O","expanded_url":"https:\/\/reut.rs\/2KH8tX5","display_url":"reut.rs\/2KH8tX5","indices":[144,167]}],"user_mentions":[{"screen_name":"eculliford","name":"Elizabeth Culliford","id":543807018,"id_str":"543807018","indices":[171,182]}],"symbols":[],"media":[{"id":1197391952616005634,"id_str":"1197391952616005634","indices":[183,206],"media_url":"http:\/\/pbs.twimg.com\/media\/EJ39owiWoAIJP5Z.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/EJ39owiWoAIJP5Z.jpg","url":"https:\/\/t.co\/O7i5uw9fFU","display_url":"pic.twitter.com\/O7i5uw9fFU","expanded_url":"https:\/\/twitter.com\/Reuters\/status\/1197391954818019328\/photo\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":800,"h":533,"resize":"fit"},"small":{"w":680,"h":453,"resize":"fit"},"large":{"w":800,"h":533,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":1197391952616005634,"id_str":"1197391952616005634","indices":[183,206],"media_url":"http:\/\/pbs.twimg.com\/media\/EJ39owiWoAIJP5Z.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/EJ39owiWoAIJP5Z.jpg","url":"https:\/\/t.co\/O7i5uw9fFU","display_url":"pic.twitter.com\/O7i5uw9fFU","expanded_url":"https:\/\/twitter.com\/Reuters\/status\/1197391954818019328\/photo\/1","type":"photo","sizes":{"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":800,"h":533,"resize":"fit"},"small":{"w":680,"h":453,"resize":"fit"},"large":{"w":800,"h":533,"resize":"fit"}}}]}},"quote_count":2,"reply_count":3,"retweet_count":5,"favorite_count":15,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/J1Dt70ARvG","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1197391954818019328","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[117,140]}],"user_mentions":[],"symbols":[]},"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":"Reuters","name":"Reuters","id":1652541,"id_str":"1652541","indices":[3,11]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1574315567168"}
python json pandas tweepy
1个回答
0
投票

您不必将文本文件转换为json即可将其读取为熊猫数据框:

pd.read_json('yourfile.txt')

它应该工作。

© www.soinside.com 2019 - 2024. All rights reserved.