如何解析txt文件中的信息? Python 3.0

时间:2016-11-15 05:00:10

标签: json python-3.x parsing

这只是代码示例

{
  "created_at": "Fri Jan 31 05:51:59 +0000 2014",
  "favorited": false,
  "lang": "en",
  "place": {
    "country_code": "US",
    "url": "https://api.twitter.com/1.1/geo/id/cf44347a08102884.json"
  },
  "retweeted": false,
  "source": "<a href=\"http://tapbots.com/software/tweetbot/mac\" rel=\"nofollow\">Tweetbot for Mac</a>",
  "text": "Active crime scene on I-59/20 near Jeff/Tusc Co line. One dead, one injured; shooting involved. Police search in the area; traffic stopped",
  "truncated": false
}

如何在python中解析此问题,以便我可以在textlang中获取相关信息?

1 个答案:

答案 0 :(得分:0)

我假设此片段不完整,因为它看起来像json但目前无效。假设有效的json文档,您可以使用json模块:

>>> import json
>>> s = """{"lang": "en", "favorited": false, "truncated": false, ... }"""
>>> data = json.loads(s)
>>> data['lang']
'en'
>>> data['text']
'Active crime scene on I-59/20 near Jeff/Tusc Co line. One dead, one injured; shooting involved. Police search in the area; traffic stopped'