试图通过与ElementTree的功能指数LXML没有输出
问题描述:
from lxml import etree
from xml.etree import cElementTree as ET
tree = ET.parse(r"D:\General\Python_Preference_validation\sample.xml")
root = tree.getroot()
root[0][1].text
这里获取元素的值是XML
文件:试图通过与ElementTree的功能指数LXML没有输出
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我期待着得到输出为2008,但是当我运行该脚本,它不提供任何输出或抱怨任何语法错误。粘贴代码和XML以及
答
也许你刚刚到print
print root[0][1].text # output '2008'
+0
多亏了它简单的平面列表.. .. :) – Yogi
+0
不客气,随时标记为接受,如果它有帮助。 – floatingpurr
XML被验证是存在于一些目录的位置。 – Yogi
本地测试,它工作正常。如果你尝试这样做会发生什么:'for root in root:print r'? – floatingpurr
@floatingpurr for循环给我 我想全年元素的对比 – Yogi