Python 2.7 Python从文本行提取数据
我从一个网页上得到了以下信息。这应该有很多行,出现在[]中,从{“EffectiveTime”…到“SystemLoad”}我想从所有这些标记中提取值。。。有人能建议正确的方法吗?有这么多行及其重复出现Python 2.7 Python从文本行提取数据,python-2.7,Python 2.7,我从一个网页上得到了以下信息。这应该有很多行,出现在[]中,从{“EffectiveTime”…到“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":
{"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},
让我们从
[]
字符开始定义输入字符串(跳过调试部分)
它直接是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)
让我们从
[]
字符开始定义输入字符串(跳过调试部分)
它直接是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)
如何将其应用于所有行?如何将其应用于所有行?非常感谢!!非常感谢你!!