使用python从XML读取数据

2024-09-27 21:23:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我从网上收到的数据如下:

<collective2>
    <status>OK</status>
        <positionstatus>
        <calctime>2017-10-17 03:25:57:000</calctime>
        <symbol>AA</symbol>
        <position>102</position>
        <averagecost>48.86</averagecost>
    </positionstatus>
</collective2>

有什么帮助吗。例如get'OK',。。。也叫“AA” 提前谢谢。在

我试过:

^{pr2}$

Tags: 数据getstatuspositionoksymbolaapr2
2条回答

您并没有给出很多关于您需要什么的信息,只是简单地阅读xml.etree documentation的前几段就应该告诉您,下面的代码为您提供了所需的结果:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ET

tree = ET.parse('data.xml')

print(tree.find('status').text)                         # OK
print(tree.find('positionstatus').find('symbol').text)  # AA

希望这有帮助。在

这是我的方法:

>>> from lxml import html as HTML
>>> data1 = HTML.fromstring(data)
print data1.xpath('//status')[0].text
OK
>>> print data1.xpath('//symbol')[0].text
AA

相关问题 更多 >

    热门问题