如何将此数据转换为json格式并存储在json fi中

2024-04-26 10:45:53 发布

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

所以我有这样格式的样本数据:

['{"created_at":"Sat Mar 16 16:10:43 +0000 2019","id":1106950932569288707,"id_str":"1106950932569288707","text":"Hillary and her #nitwit adherents claim she won the 2016 election. \"Participation trophies\" don\'t mean a thing in t\u2026 https:\/\/t.co\/zUUSMWXqja","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":864531280934907905,"id_str":"864531280934907905","name":"\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8 SSDD \ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8","screen_name":"EdGullion","location":"Objective Reality","url":null,"description":"\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8#Trump | #BuildThatWall | #AmericaFirst | \ud83d\udeabLibs | \ud83d\udeabDems | Block #Nitwits | http:\/\/Gab.com \ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8","translator_type":"none","protected":false,"verified":false,"followers_count":5173,"friends_count":5171,"listed_count":4,"favourites_count":18478,"statuses_count":41837,"created_at":"Tue May 16 17:21:34 +0000 2017","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","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\/959783845246615552\/nJBLKbFS_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/959783845246615552\/nJBLKbFS_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/864531280934907905\/1541463166","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"quoted_status_id":1106777688574849024,"quoted_status_id_str":"1106777688574849024","quoted_status":{"created_at":"Sat Mar 16 04:42:18 +0000 2019","id":1106777688574849024,"id_str":"1106777688574849024","text":"Stacey Abrams claimed that she won her election against Republican Gov. Brian Kemp, during an event yesterday.\n\n\u201cI\u2026 https:\/\/t.co\/swEmfwTo8S","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":835676806455832576,"id_str":"835676806455832576","name":"\ud83c\uddfa\ud83c\uddf8 Jack Ralph \ud83c\uddfa\ud83c\uddf8","screen_name":"NevadaJack2","location":"Carson City, NV","url":null,"description":"Vietnam Era #Veteran (USAF 61-65), married 57 years, father of 4, grand of 7, retired IT guy. YUUGE #Trump supporter. #MAGA #TRUMP2020 #WWG1WGA","translator_type":"none","protected":false,"verified":false,"followers_count":59620,"friends_count":57299,"listed_count":55,"favourites_count":213014,"statuses_count":125563,"created_at":"Sun Feb 26 02:24:11 +0000 2017","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","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":"E81C4F","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\/852694196804780034\/Ylah4YQT_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/852694196804780034\/Ylah4YQT_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/835676806455832576\/1492132580","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":"Stacey Abrams claimed that she won her election against Republican Gov. Brian Kemp, during an event yesterday.\n\n\u201cI did win my election,\u201d she said, according to ABC News reporter Adam Kelsey. \u201cI just didn\u2019t get to have the job.\u201d \n\nhttps:\/\/t.co\/HCyafbiNw2","display_text_range":[0,253],"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/HCyafbiNw2","expanded_url":"https:\/\/dailycaller.com\/2019\/03\/15\/stacey-abrams-georgia-election\/","display_url":"dailycaller.com\/2019\/03\/15\/sta\u2026","indices":[230,253]}],"user_mentions":[],"symbols":[]}},"quote_count":184,"reply_count":708,"retweet_count":411,"favorite_count":473,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/swEmfwTo8S","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1106777688574849024","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[116,139]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"quoted_status_permalink":{"url":"https:\/\/t.co\/K7j2fKQy1g","expanded":"https:\/\/twitter.com\/NevadaJack2\/status\/1106777688574849024","display":"twitter.com\/NevadaJack2\/st\u2026"},"is_quote_status":true,"extended_tweet":{"full_text":"Hillary and her #nitwit adherents claim she won the 2016 election. \"Participation trophies\" don\'t mean a thing in the real world. What a screw up generation the Left has loosed on our nation.","display_text_range":[0,191],"entities":{"hashtags":[{"text":"nitwit","indices":[16,23]}],"urls":[],"user_mentions":[],"symbols":[]}},"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"nitwit","indices":[16,23]}],"urls":[{"url":"https:\/\/t.co\/zUUSMWXqja","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1106950932569288707","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[117,140]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1552752643135"}', '{"created_at":"Sat Mar 16 16:10:43 +0000 2019","id":1106950932569288707,"id_str":"1106950932569288707","text":"Hillary and her #nitwit adherents claim she won the 2016 election. \"Participation trophies\" don\'t mean a thing in t\u2026 https:\/\/t.co\/zUUSMWXqja","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":864531280934907905,"id_str":"864531280934907905","name":"\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8 SSDD \ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8","screen_name":"EdGullion","location":"Objective Reality","url":null,"description":"\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8#Trump | #BuildThatWall | #AmericaFirst | \ud83d\udeabLibs | \ud83d\udeabDems | Block #Nitwits | http:\/\/Gab.com \ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8","translator_type":"none","protected":false,"verified":false,"followers_count":5173,"friends_count":5171,"listed_count":4,"favourites_count":18478,"statuses_count":41837,"created_at":"Tue May 16 17:21:34 +0000 2017","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","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\/959783845246615552\/nJBLKbFS_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/959783845246615552\/nJBLKbFS_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/864531280934907905\/1541463166","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"quoted_status_id":1106777688574849024,"quoted_status_id_str":"1106777688574849024","quoted_status":{"created_at":"Sat Mar 16 04:42:18 +0000 2019","id":1106777688574849024,"id_str":"1106777688574849024","text":"Stacey Abrams claimed that she won her election against Republican Gov. Brian Kemp, during an event yesterday.\n\n\u201cI\u2026 https:\/\/t.co\/swEmfwTo8S","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":835676806455832576,"id_str":"835676806455832576","name":"\ud83c\uddfa\ud83c\uddf8 Jack Ralph \ud83c\uddfa\ud83c\uddf8","screen_name":"NevadaJack2","location":"Carson City, NV","url":null,"description":"Vietnam Era #Veteran (USAF 61-65), married 57 years, father of 4, grand of 7, retired IT guy. YUUGE #Trump supporter. #MAGA #TRUMP2020 #WWG1WGA","translator_type":"none","protected":false,"verified":false,"followers_count":59620,"friends_count":57299,"listed_count":55,"favourites_count":213014,"statuses_count":125563,"created_at":"Sun Feb 26 02:24:11 +0000 2017","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","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":"E81C4F","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\/852694196804780034\/Ylah4YQT_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/852694196804780034\/Ylah4YQT_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/835676806455832576\/1492132580","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":"Stacey Abrams claimed that she won her election against Republican Gov. Brian Kemp, during an event yesterday.\n\n\u201cI did win my election,\u201d she said, according to ABC News reporter Adam Kelsey. \u201cI just didn\u2019t get to have the job.\u201d \n\nhttps:\/\/t.co\/HCyafbiNw2","display_text_range":[0,253],"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/HCyafbiNw2","expanded_url":"https:\/\/dailycaller.com\/2019\/03\/15\/stacey-abrams-georgia-election\/","display_url":"dailycaller.com\/2019\/03\/15\/sta\u2026","indices":[230,253]}],"user_mentions":[],"symbols":[]}},"quote_count":184,"reply_count":708,"retweet_count":411,"favorite_count":473,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/swEmfwTo8S","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1106777688574849024","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[116,139]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"quoted_status_permalink":{"url":"https:\/\/t.co\/K7j2fKQy1g","expanded":"https:\/\/twitter.com\/NevadaJack2\/status\/1106777688574849024","display":"twitter.com\/NevadaJack2\/st\u2026"},"is_quote_status":true,"extended_tweet":{"full_text":"Hillary and her #nitwit adherents claim she won the 2016 election. \"Participation trophies\" don\'t mean a thing in the real world. What a screw up generation the Left has loosed on our nation.","display_text_range":[0,191],"entities":{"hashtags":[{"text":"nitwit","indices":[16,23]}],"urls":[],"user_mentions":[],"symbols":[]}},"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"nitwit","indices":[16,23]}],"urls":[{"url":"https:\/\/t.co\/zUUSMWXqja","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1106950932569288707","display_url":"twitter.com\/i\/web\/status\/1\u2026","indices":[117,140]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1552752643135"}']

如何将其转换为json格式并存储在json文件中?你知道吗


Tags: totextinhttpsimagecomidfalse
2条回答

使用json.loadsjson.dump

import json

# 'data' is in the same format you posted
path = "myjson.json"
json.dump([json.loads(d) for d in data], open(path, "w"))

或者简单地说:

with open(path, "w") as f:
    f.write("[{}]".format(",".join(data)))

您可以使用“json”模块

    import json

    group_data = {
        "A": "A",
        "B": "B",
        "C": "C"
    }

    with open("out.json", "w", encoding="utf-8") as make_file:
        json.dump(group_data, make_file, ensure_ascii=False, indent="\t")

相关问题 更多 >