【问题标题】:how do I parse info from a txt file? Python 3.0如何从 txt 文件中解析信息?蟒蛇3.0
【发布时间】:2016-11-15 05:00:09
【问题描述】:

这只是一个代码示例

{
  "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 中获取信息?

【问题讨论】:

    标签: json python-3.x parsing


    【解决方案1】:

    我假设此片段不完整,因为它看起来像 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'
    

    【讨论】:

    • txt文件和python编码是分开的:2个不同的文件。我试图(在 python 中)调用这个文本文件(称为 CrimeReport.txt)然后解析它。我该怎么做。
    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多