我有来自网页的以下输入。预计这将有很多行,从[" EffectiveTime" ...到#34; SystemLoad"}我想从所有这些标签中提取值...有人能建议正确的方法吗?有很多行及其反复出现。
{"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 :(得分:0)
让我们从[]
字符开始定义输入字符串(跳过调试部分)
直接json数据:反序列化为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)