【发布时间】:2015-04-21 12:54:59
【问题描述】:
我有这个 XML 文件,我想获取名称中包含“in”模式的国家节点。
<?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>
我试过了
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
list=root.find(".//country[contains(@name, 'Pana')]")
但我收到一个错误:SyntaxError: invalid predicate
谁能帮忙解决这个问题?
【问题讨论】: