使用从lxml xpath命令获得的数据填充Python列表 [英] Populating Python list using data obtained from lxml xpath command

查看:90
本文介绍了使用从lxml xpath命令获得的数据填充Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从专用服务器读取仪器数据,该服务器以xml格式提供信息.我写的代码是: 从lxml导入etree作为ET

I'm reading instrument data from a specialty server that delivers the info in xml format. The code I've written is: from lxml import etree as ET

xmlDoc = ET.parse('http://192.168.1.198/Bench_read.xml')
print ET.tostring(xmlDoc, pretty_print=True)

dmtCount = xmlDoc.xpath('//dmt')
print(len(dmtCount))

dmtVal = []

for i in range(1, len(dmtCount)):
    dmtVal[i:0] = xmlDoc.xpath('./address/text()')
    dmtVal[i:1] = xmlDoc.xpath('./status/text()')
    dmtVal[i:2] = xmlDoc.xpath('./flow/text()')
    dmtVal[i:3] = xmlDoc.xpath('./dp/text()')
    dmtVal[i:4] = xmlDoc.xpath('./inPressure/text()')
    dmtVal[i:5] = xmlDoc.xpath('./actVal/text()')
    dmtVal[i:6] = xmlDoc.xpath('./temp/text()')
    dmtVal[i:7] = xmlDoc.xpath('./valveOnPercent/text()')

print dmtVal

我得到的结果是:

$python XMLparse2.py
<response>

<heartbeat>0x24</heartbeat>

<dmt node="1">

    <address>0x21</address>
    <status>0x01</status>
    <flow>0.000000</flow>
    <dp>0.000000</dp>
    <inPressure>0.000000</inPressure>
    <actVal>0.000000</actVal>
    <temp>0x00</temp>
    <valveOnPercent>0x00</valveOnPercent>

</dmt>

<dmt node="2">

    <address>0x32</address>
    <status>0x01</status>
    <flow>0.000000</flow>
    <dp>0.000000</dp>
    <inPressure>0.000000</inPressure>
    <actVal>0.000000</actVal>
    <temp>0x00</temp>
    <valveOnPercent>0x00</valveOnPercent>

</dmt>

</response>

...Starting to parse XML nodes
2
[]
...Done

太棒了,什么都没出来.我尝试在xpath调用中使用/value代替/text(),但是结果保持不变.是我的问题吗?

Sooo, nothing is coming out. I've tried using /value in place of the /text() in the xpath call, but the results are unchanged. Is my problem:

1)for循环中的xpath命令不正确?或

1) An incorrect xpath command in the for loop? or

2)我构造列表变量dmtVal的方式有问题吗?或

2) A problem in the way I've structured list variable dmtVal ? or

3)我完全想念的其他东西了吗?

3) Something else I'm missing completely?

我欢迎任何建议!预先感谢...

I'd welcome any suggestions! Thanks in advance...

推荐答案

<dmt/>标签进行计数,然后按索引对其进行迭代既低效又不符合Python规范.除此之外,您使用错误的语法(切片而不是索引)来索引数组.实际上,您根本不需要为val编制索引,以Python方式使用列表推导即可实现.

Counting <dmt/> tags and then iterating over them by index is both inefficient and un-Pythonic. Apart from that you are using wrong syntax (slice instead of index) for indexing arrays. In fact you don't need to index the val at all, to do it Pythonic way use list comprehensions.

这是stranac建议的稍作修改的版本:

Here's a slightly modified version of what stranac suggested:

from lxml import etree as ET

xmlDoc = ET.parse('http://192.168.1.198/Bench_read.xml')
print ET.tostring(xmlDoc, pretty_print=True)

response = xmlDoc.getroot()

tags = (
    'address',
    'status',
    'flow',
    'dp',
    'inPressure',
    'actVal',
    'temp',
    'valveOnPercent',
)

dmtVal = []

for dmt in response.iter('dmt'):
    val = [dmt.xpath('./%s/text()' % tag) for tag in tags]
    dmtVal.append(val)

这篇关于使用从lxml xpath命令获得的数据填充Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆