2016-12-06 17 views
1

私はWebページから次の入力を受けています。これは、{"EffectiveTime" ... "SystemLoad"から始まる[]内にたくさんの行が存在することが予想されます。これらのタグから値を抽出したいのですが...誰でも正しい方法を提案できますか?非常に多くの行とその繰り返しがあります。Python-Text行からのデータの抽出

{"Debug":"Key:01-Oct-2016 04:00:00_04-Dec-2016 23:45:00, 
Age:-1","ErrorMessage":null,"LastUpdated":"06-Dec-2016 15:14:14","Rows": 
[{"EffectiveTime":"01-Oct-2016 
04:00:00","EurPrice":30.460,"GbpPrice":26.260,"RunType":"EP2","SystemLoad":2702. 
651},{"EffectiveTime":"01-Oct-2016 
04:30:00","EurPrice":30.460,"GbpPrice":26.260,"RunType":"EP2","SystemLoad":2694. 
620},{"EffectiveTime":"01-Oct-2016 
05:00:00","EurPrice":30.510,"GbpPrice":26.300,"RunType":"EP2","SystemLoad":2718. 
430}, 
+0

'json.loads()'? –

+0

これをすべての行にどのように適用するのですか? –

答えて

0

のは、それが直接JSONデータだ

(デバッグ部分をスキップ)[]文字から始まる入力文字列を定義してみましょう:Pythonの辞書のリストにunserializes

import json 

text='''[{"EffectiveTime":"01-Oct-2016 04:00:00","EurPrice":30.460,"GbpPrice":26.260, 
    "RunType":"EP2","SystemLoad":2702.651},{"EffectiveTime":"01-Oct-2016 04:30:00","EurPrice":30.460,"GbpPrice":26.260,"RunType":"EP2", 
     "SystemLoad":2694.620},{"EffectiveTime":"01-Oct-2016 05:00:00","EurPrice":30.510,"GbpPrice":26.300,"RunType":"EP2","SystemLoad":2718.430}]''' 

dict_list = json.loads(text) 
for d in dict_list: 
    print(d) 

結果:

{u'GbpPrice': 26.26, u'EffectiveTime': u'01-Oct-2016 04:00:00', u'EurPrice': 30.46, u'SystemLoad': 2702.651, u'RunType': u'EP2'} 
{u'GbpPrice': 26.26, u'EffectiveTime': u'01-Oct-2016 04:30:00', u'EurPrice': 30.46, u'SystemLoad': 2694.62, u'RunType': u'EP2'} 
{u'GbpPrice': 26.3, u'EffectiveTime': u'01-Oct-2016 05:00:00', u'EurPrice': 30.51, u'SystemLoad': 2718.43, u'RunType': u'EP2'} 

フル・ダンプでも同じ結果が得られますデバッグヘッダーを含む)は次のようになります。

import json 

text='''{"Debug":"Key:01-Oct-2016 04:00:00_04-Dec-2016 23:45:00,Age:-1","ErrorMessage":null,"LastUpdated":"06-Dec-2016 15:14:14","Rows": 
[{"EffectiveTime":"01-Oct-2016 04:00:00","EurPrice":30.460,"GbpPrice":26.260,"RunType":"EP2","SystemLoad":2702.651}, 
{"EffectiveTime":"01-Oct-2016 04:30:00","EurPrice":30.460,"GbpPrice":26.260,"RunType":"EP2","SystemLoad":2694.620},{"EffectiveTime":"01-Oct-2016 05:00:00","EurPrice":30.510,"GbpPrice":26.300,"RunType":"EP2","SystemLoad":2718.430}]}''' 

main_dict = json.loads(text) 
for d in main_dict["Rows"]: # access "Rows" member 
    print(d) 
+0

ありがとうございました! –

関連する問題