Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从XML读取数据_Python_Xml - Fatal编程技术网

使用python从XML读取数据

使用python从XML读取数据,python,xml,Python,Xml,我从网上收到的数据如下: <collective2> <status>OK</status> <positionstatus> <calctime>2017-10-17 03:25:57:000</calctime> <symbol>AA</symbol> <position>102</position>

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

<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>

您并没有提供很多关于所需内容的详细信息,只是简单地阅读了本手册的前几段就可以知道以下代码为您提供了所需的结果:

#!/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

您试图解析XML的库有哪些?将XML.etree.ElementTree作为eta导入根据,ElementTree对象没有
get
方法。尝试使用
查找
>>> from lxml import html as HTML
>>> data1 = HTML.fromstring(data)
print data1.xpath('//status')[0].text
OK
>>> print data1.xpath('//symbol')[0].text
AA