我已经在json文件中抓取了一些数据,但是由于Extra Data Error我无法加载它?我可以知道可以用什么方法解决吗?
我的代码是
with open('liverpool.json', 'r') as read:
data = json.loads(read.read())
print(data['text'])
我的JSON文件是
{"created_at":"Thu May 09 03:48:38 +0000 2019","id":1126333127452794881,"id_str":"1126333127452794881","text":"RT @andihiyat: UCL\nLiverpool lawan tottenham\n\nUEL\nArsenal lawan chelsea\n\nMU jadi penonton bayaran aja udah daripada ngga ada kerjaan","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":89329977,"id_str":"89329977","name":"whatever it takes","screen_name":"ikhsanrnldy","location":null,"url":null,"description":null,"translator_type":"none","protected":false,"verified":false,"followers_count":370,"friends_count":331,"listed_count":0,"favourites_count":76,"statuses_count":16478,"created_at":"Thu Nov 12 00:43:34 +0000 2009","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"01010F","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":"0084B4","profile_sidebar_border_color":"A8C7F7","profile_sidebar_fill_color":"C0DFEC","profile_text_color":"540AF5","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1121250950570582022\/x9GrAczT_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1121250950570582022\/x9GrAczT_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/89329977\/1556161880","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 May 08 21:27:39 +0000 2019","id":1126237248783917056,"id_str":"1126237248783917056","text":"UCL\nLiverpool lawan tottenham\n\nUEL\nArsenal lawan chelsea\n\nMU jadi penonton bayaran aja udah daripada ngga ada kerjaan","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":886934161,"id_str":"886934161","name":"andihiyat","screen_name":"andihiyat","location":null,"url":null,"description":"Malu bercanda sesat di jalan | http:\/\/instagram.com\/andihiyat | andihiyat@gmail.com","translator_type":"none","protected":false,"verified":false,"followers_count":606131,"friends_count":545,"listed_count":226,"favourites_count":1979,"statuses_count":31038,"created_at":"Wed Oct 17 14:26:34 +0000 2012","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"id","contributors_enabled":false,"is_translator":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_link_color":"009999","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1114013770072780800\/vutmo5hd_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1114013770072780800\/vutmo5hd_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/886934161\/1547662374","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,"quote_count":222,"reply_count":370,"retweet_count":1715,"favorite_count":4202,"entities":{"hashtags":[],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"in"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"andihiyat","name":"andihiyat","id":886934161,"id_str":"886934161","indices":[3,13]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"in","timestamp_ms":"1557373718499"}
{"created_at":"Thu May 09 04:02:53 +0000 2019","id":1126336711649366017,"id_str":"1126336711649366017","text":"RT @Omojuwa: A Barcelona x Ajax final became a Liverpool x Tottenham final. Whatever you are going through, you are not down and out. Pleas\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":3311255787,"id_str":"3311255787","name":"Man in caftan \ud83d\udc73\ud83c\udffd\u200d\u2642\ufe0f","screen_name":"muhd_burga","location":"North, NG","url":"http:\/\/instagram.com\/moomtaz_ng","description":"knowledge seeker \ud83d\udcda| Part time entrepreneur\ud83d\udcca| FC BAR\u00c7A\u26bd|\nBussiness account\ud83d\udc47\ud83c\udffb","translator_type":"none","protected":false,"verified":false,"followers_count":612,"friends_count":437,"listed_count":1,"favourites_count":6566,"statuses_count":6603,"created_at":"Sun Jun 07 00:22:41 +0000 2015","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\/1105581178629832704\/h0J3Rd22_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1105581178629832704\/h0J3Rd22_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/3311255787\/1555512617","default_profile":true,"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 May 08 21:06:25 +0000 2019","id":1126231906444558337,"id_str":"1126231906444558337","text":"A Barcelona x Ajax final became a Liverpool x Tottenham final. Whatever you are going through, you are not down and\u2026 https:\/\/t.co\/LIw44MJXEq","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\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":115628224,"id_str":"115628224","name":"JJ. Omojuwa","screen_name":"Omojuwa","location":"The skies | jj@omojuwa.com","url":"http:\/\/en.wikipedia.org\/wiki\/Japheth_J._Omojuwa","description":"Non nobis, sed Omnibus.","translator_type":"none","protected":false,"verified":true,"followers_count":654440,"friends_count":3331,"listed_count":1364,"favourites_count":16003,"statuses_count":530374,"created_at":"Fri Feb 19 10:01:16 +0000 2010","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"6D63FF","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme7\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme7\/bg.gif","profile_background_tile":true,"profile_link_color":"E81C4F","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"7AC3EE","profile_text_color":"3D1957","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1116486680448569344\/wNthfONq_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1116486680448569344\/wNthfONq_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/115628224\/1553848557","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":"A Barcelona x Ajax final became a Liverpool x Tottenham final. Whatever you are going through, you are not down and out. Please, never give up! Never ever giving up after what I have seen the last 24 hours.","display_text_range":[0,206],"entities":{"hashtags":[],"urls":[],"user_mentions":[],"symbols":[]}},"quote_count":56,"reply_count":70,"retweet_count":2239,"favorite_count":4287,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/LIw44MJXEq","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1126231906444558337","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[117,140]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":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":"Omojuwa","name":"JJ. Omojuwa","id":115628224,"id_str":"115628224","indices":[3,11]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1557374573038"}
{"created_at":"Thu May 09 04:02:53 +0000 2019","id":1126336712198717440,"id_str":"1126336712198717440","text":"RT @LVPibai: El United eliminando al PSG en el 94\nLa Juve remontando al Atleti\nEl Ajax carg\u00e1ndose al campe\u00f3n de Europa y la Juve de Cristia\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":2490066259,"id_str":"2490066259","name":"el penzil","screen_name":"p23nzil","location":"Valencia","url":"https:\/\/www.twitch.tv\/no23_","description":"Mi imaginaci\u00f3n no tiene l\u00edmites.","translator_type":"none","protected":false,"verified":false,"followers_count":88,"friends_count":410,"listed_count":1,"favourites_count":1654,"statuses_count":2618,"created_at":"Sun May 11 15:50:58 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"es","contributors_enabled":false,"is_translator":false,"profile_background_color":"1BE01B","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_link_color":"19CF86","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\/1051623579043987457\/lDmHd_9b_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1051623579043987457\/lDmHd_9b_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2490066259\/1465492298","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 May 08 21:00:49 +0000 2019","id":1126230495954702337,"id_str":"1126230495954702337","text":"El United eliminando al PSG en el 94\nLa Juve remontando al Atleti\nEl Ajax carg\u00e1ndose al campe\u00f3n de Europa y la Juve\u2026 https:\/\/t.co\/WUoh4FtBTF","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\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":2754746065,"id_str":"2754746065","name":"Ibai","screen_name":"LVPibai","location":"Barcelona, Espa\u00f1a","url":"http:\/\/twitch.tv\/ibaailvp","description":"Comentarista profesional trabajando para @LVPes. Tengo 23 a\u00f1os.","translator_type":"regular","protected":false,"verified":true,"followers_count":650201,"friends_count":545,"listed_count":897,"favourites_count":37987,"statuses_count":50886,"created_at":"Fri Aug 22 11:50:45 +0000 2014","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"es","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","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":"000000","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1102578175983407104\/jtxwZRZd_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1102578175983407104\/jtxwZRZd_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/2754746065\/1528424597","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":"El United eliminando al PSG en el 94\nLa Juve remontando al Atleti\nEl Ajax carg\u00e1ndose al campe\u00f3n de Europa y la Juve de Cristiano \nEl City eliminado despu\u00e9s de celebrar un gol en el 93\nEl Liverpool remontando un 3-0 \nEl Ajax eliminado en el 95\n\nLa mejor Champions de la historia.","display_text_range":[0,278],"entities":{"hashtags":[],"urls":[],"user_mentions":[],"symbols":[]}},"quote_count":405,"reply_count":278,"retweet_count":23636,"favorite_count":60937,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/WUoh4FtBTF","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1126230495954702337","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[117,140]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"es"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[],"user_mentions":[{"screen_name":"LVPibai","name":"Ibai","id":2754746065,"id_str":"2754746065","indices":[3,11]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"es","timestamp_ms":"1557374573169"}
错误显示:
raise JSONDecodeError("Extra data", s, end)
JSONDecodeError: Extra data
答案 0 :(得分:0)
您可以这样做:
import json
with open('liverpool.json', 'r') as read:
data = json.loads(read.readline())
print(data['text'])
注意,我将read.read()
更改为read.readline()
。这是因为您的json中有多个文档。
答案 1 :(得分:0)
这里的问题是您的json文件包含多个字典。 json.loads()
期望单个字典或字典列表。解决方案是将所有json字典包装在一个列表中,然后在该字典列表上使用json.loads()
。
json.loads([dict1, dict2])
答案 2 :(得分:0)
您的JSON文件包含有效的JSON 每行,而整个文件不是JSON。逐行阅读它们:
for line in open('liverpool.json'):
# be aware of empty lines
if not line.strip():
continue
data = json.loads(line)
print(data['text'])
这是有效的JSON:
[{"key1": "val1"}, {"key2": "val2"}]
这不是:
{"key1": "val1"}
{"key2": "val2"}