2016-11-15 35 views
-1

這僅僅是一個代碼示例如何解析txt文件中的信息? Python的3.0

{ 
    "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的信息?

回答

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' 
+0

txt文件和蟒蛇編碼是相互獨立的2個不同的文件。我試圖調用(在Python中)這個文本文件(稱爲CrimeReport.txt),然後解析它。我將如何做到這一點。 – kel