【问题标题】:Python-Extract data from text rowsPython-从文本行中提取数据
【发布时间】:2016-12-06 15:53:29
【问题描述】:

我有来自网页的以下输入。并且预计会有很多行,从 {"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":"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},

【问题讨论】:

  • 如何将其应用于所有行?

标签: python-2.7


【解决方案1】:

让我们定义从[] 字符开始的输入字符串(跳过调试部分)

直接是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)

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 2014-11-10
相关资源
最近更新 更多