如何修复或清理损坏的JSON文件

2024-06-17 15:02:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个损坏的JSON文件,用于公共推文:

"{\"created_at\":\"Thu Feb 25 05:05:41 +0000 2021\",\"id\":1364803731678715907,\"id_str\":\"1364803731678715907\",\"text\":\"\\u201cMe solta crlh\\u201d\",\"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\":870120437132529664,\"id_str\":\"870120437132529664\",\"name\":\"vraulentina\",\"screen_name\":\"florescabrall\",\"location\":\"Dourados, Brasil\",\"url\":null,\"description\":\"hmm, sei la\",\"translator_type\":\"none\",\"protected\":false,\"verified\":false,\"followers_count\":336,\"friends_count\":339,\"listed_count\":0,\"favourites_count\":32475,\"statuses_count\":16889,\"created_at\":\"Thu Jun 01 03:30:53 +0000 2017\",\"utc_offset\":null,\"time_zone\":null,\"geo_enabled\":true,\"lang\":null,\"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\":\"1B95E0\",\"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\\/1364803281785085954\\/ITyuznjA_normal.jpg\",\"profile_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_images\\/1364803281785085954\\/ITyuznjA_normal.jpg\",\"profile_banner_url\":\"https:\\/\\/pbs.twimg.com\\/profile_banners\\/870120437132529664\\/1554819329\",\"default_profile\":false,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null},\"geo\":null,\"coordinates\":null,\"place\":{\"id\":\"9ae7de89f4e0fcc0\",\"url\":\"https:\\/\\/api.twitter.com\\/1.1\\/geo\\/id\\/9ae7de89f4e0fcc0.json\",\"place_type\":\"city\",\"name\":\"Dourados\",\"full_name\":\"Dourados, Brasil\",\"country_code\":\"BR\",\"country\":\"Brasil\",\"bounding_box\":{\"type\":\"Polygon\",\"coordinates\":[[[-55.521651,-22.441805],[-55.521651,-21.880052],[-54.201264,-21.880052],[-54.201264,-22.441805]]]},\"attributes\":{}},\"contributors\":null,\"is_quote_status\":false,\"quote_count\":0,\"reply_count\":0,\"retweet_count\":0,\"favorite_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[],\"symbols\":[]},\"favorited\":false,\"retweeted\":false,\"filter_level\":\"low\",\"lang\":\"pt\",\"timestamp_ms\":\"1614229541116\"}\r\n"

最终,我只需要提供文本和坐标。我在代码中使用StreamListener将tweet收集为JSON文件,并将它们发送到我计算机上的文件位置。JSON文件是实时收集的,但当我打开它们时,它们看起来是这样的,不可读或无法用于映射。有没有人能提供一些关于代码的见解,以便以可读的格式实时收集推文,或者如何修复我得到的JSON文件?多谢各位


Tags: 文件tonameinhttpsimagecomid
2条回答

如果您的意思是您意外地将repr()的JSON保存到了一个文件中,那么您可以使用ast.literal_eval()取消repr()它:

import ast
import json

file = ...
json_string = ast.literal_eval(file.read())
json_data = json.loads(json_string)
assert isinstance(json_data, dict)

相关问题 更多 >