如何使用python从json格式的txt文件中提取键值

如何使用python从json格式的txt文件中提取键值,python,json,Python,Json,输出: { "delete": { "status": { "id": 294512601600258048, "id_str": "294512601600258048", "user_id": 90681582, "user_id_str": "90681582" }, "timestamp_ms": "1410368494083" } }{ "created_at": "Wed Sep 10 17:01:3

输出:

{
  "delete": {
    "status": {
      "id": 294512601600258048,
      "id_str": "294512601600258048",
      "user_id": 90681582,
      "user_id_str": "90681582"
    },
    "timestamp_ms": "1410368494083"
  }
}{
  "created_at": "Wed Sep 10 17:01:33 +0000 2014",
  "id": 509748524897292288,
  "id_str": "509748524897292288",
  "text": "@Brenamae_ I WHALE SLAP YOUR FIN AND TELL YOU ONE LAST TIME: GO AWHALE",
  "source": "\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e",
  "truncated": false,
  "in_reply_to_status_id": 509748106015948800,
  "in_reply_to_status_id_str": "509748106015948800",
  "in_reply_to_user_id": 242563886,
  "in_reply_to_user_id_str": "242563886",
  "in_reply_to_screen_name": "Brenamae_",
  "user": {
    "id": 175160659,
    "id_str": "175160659",
    "name": "Butterfly",
    "screen_name": "VanessaLilyWan",
    "location": "Canada, Montreal",
    "url": "http:\/\/instagram.com\/vanessalilywan",
    "description": "British youtubers. 'Nuff said.",
    "protected": false,
    "verified": false,
    "followers_count": 118,
    "friends_count": 180,
    "listed_count": 2,
    "favourites_count": 319,
    "statuses_count": 10221,
    "created_at": "Thu Aug 05 20:03:16 +0000 2010",
    "utc_offset": -36000,
    "time_zone": "Hawaii",
    "geo_enabled": false,
    "lang": "en",
    "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\/470701406245376000\/2aXDrauR_normal.jpeg",
    "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/470701406245376000\/2aXDrauR_normal.jpeg",
    "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/175160659\/1404361640",
    "default_profile": false,
    "default_profile_image": false,
    "following": null,
    "follow_request_sent": null,
    "notifications": null
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "retweet_count": 0,
  "favorite_count": 0,
  "entities": {
    "hashtags": [

    ],
    "trends": [

    ],
    "urls": [

    ],
    "user_mentions": [
      {
        "screen_name": "Brenamae_",
        "name": "I-G-G-Bye",
        "id": 242563886,
        "id_str": "242563886",
        "indices": [
          0,
          10
        ]
      }
    ],
    "symbols": [

    ]
  },
  "favorited": false,
  "retweeted": false,
  "possibly_sensitive": false,
  "filter_level": "medium",
  "lang": "en",
  "timestamp_ms": "1410368493668"
}

只有文本键值

和JSONBeautifier这是您的JSON代码

 @Brenamae_ I WHALE SLAP YOUR FIN AND TELL YOU ONE LAST TIME: GO AWHALE
您可以通过在代码中应用以下内容来提取文本值:

    {  
   "delete":{  
      "status":{  
         "id":294512601600258048,
         "id_str":"294512601600258048",
         "user_id":90681582,
         "user_id_str":"90681582"
      },
      "timestamp_ms":"1410368494083"
   }
}{  
   "created_at":"Wed Sep 10 17:01:33 +0000 2014",
   "id":509748524897292288,
   "id_str":"509748524897292288",
   "text":"@Brenamae_ I WHALE SLAP YOUR FIN AND TELL YOU ONE LAST TIME: GO AWHALE",
   "source":"\u003ca href=\"http://twitter.com/download/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c/a\u003e",
   "truncated":false,
   "in_reply_to_status_id":509748106015948800,
   "in_reply_to_status_id_str":"509748106015948800",
   "in_reply_to_user_id":242563886,
   "in_reply_to_user_id_str":"242563886",
   "in_reply_to_screen_name":"Brenamae_",
   "user":{  
      "id":175160659,
      "id_str":"175160659",
      "name":"Butterfly",
      "screen_name":"VanessaLilyWan",
      "location":"Canada, Montreal",
      "url":"http://instagram.com/vanessalilywan",
      "description":"British youtubers. 'Nuff said.",
      "protected":false,
      "verified":false,
      "followers_count":118,
      "friends_count":180,
      "listed_count":2,
      "favourites_count":319,
      "statuses_count":10221,
      "created_at":"Thu Aug 05 20:03:16 +0000 2010",
      "utc_offset":-36000,
      "time_zone":"Hawaii",
      "geo_enabled":false,
      "lang":"en",
      "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/470701406245376000/2aXDrauR_normal.jpeg",
      "profile_image_url_https":"https://pbs.twimg.com/profile_images/470701406245376000/2aXDrauR_normal.jpeg",
      "profile_banner_url":"https://pbs.twimg.com/profile_banners/175160659/1404361640",
      "default_profile":false,
      "default_profile_image":false,
      "following":null,
      "follow_request_sent":null,
      "notifications":null
   },
   "geo":null,
   "coordinates":null,
   "place":null,
   "contributors":null,
   "retweet_count":0,
   "favorite_count":0,
   "entities":{  
      "hashtags":[  

      ],
      "trends":[  

      ],
      "urls":[  

      ],
      "user_mentions":[  
         {  
            "screen_name":"Brenamae_",
            "name":"I-G-G-Bye",
            "id":242563886,
            "id_str":"242563886",
            "indices":[  
               0,
               10
            ]
         }
      ],
      "symbols":[  

      ]
   },
   "favorited":false,
   "retweeted":false,
   "possibly_sensitive":false,
   "filter_level":"medium",
   "lang":"en",
   "timestamp_ms":"1410368493668"
}

考虑到strjson等同于您的json数据。

您在问什么?您能格式化json/字典吗?您的json无效。最好发布一个示例,而不是所有json数据,并且格式良好。首先,我需要从文本文件中获取json格式的数据,然后仅提取文本键valueStack Overflow不是代码编写服务请保持这样。你可能想把精力花在一些问题上,在这些问题上,OP至少显示出最小的努力,即使答案很琐碎。
import json
json.loads(data)
finalStr = data["text"]