2016-12-06 117 views
1

我從網頁輸入以下內容。這預計會有很多行,出現在[]從{「EffectiveTime」...到「SystemLoad」}我想從所有這些標籤中提取值...任何人都可以提出正確的方法來做到這一點?有這麼多的行,它反覆出現。Python從文本行中提取數據

{"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數據:unserializes到Python字典的名單

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

非常感謝你! –