2016-11-15 8 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 
} 

私はtextまたはlangで情報を得ることができるようにどのように私はpythonでこれを解析しないのですか?

答えて

0

jsonのように見えますが、この部分が不完全であると思われますが、現在無効です。 2つの異なるファイル:

>>> 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ファイルやPythonのコーディングが互いに分離されている有効なjson文書と仮定すると、あなたはjsonモジュールを使用することができます。私はPythonでこのテキストファイル(CrimeReport.txtと呼ばれる)を呼び出して解析しようとしています。どうすればそれができますか? – kel

関連する問題