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