我使用XML库来解析untangle树。但是在树中,有一个名为def的元素。如果我试图访问它的值
obj = untangle.parse(pathtoxml)
entry = obj.entry_list
defn = entry.def它正在播放SyntaxError。
File "E:/python2/test/main.py", line 33
defn = entry.def
^
SyntaxError: invalid syntaxdef是Python中的一个关键字,我如何访问这个值。
这是样例树
<entry_list version="1.0">
<entry id="python">
<ew>python</ew>
<subj>ZH</subj>
<art>
<artref id="python"/>
<capt>python</capt>
<dim>75,15</dim>
</art>
<hw>py*thon</hw>
<sound>
<wav>python01.wav</wav>
<wpr>!pI-+th@n</wpr>
</sound>
<pr>ˈpī-ˌthän, -thən</pr>
<fl>noun</fl>
<et>
Latin, monstrous serpent killed by Apollo, from Greek
<it>Pythōn,</it>
from
<it>Pythō</it>
Delphi
</et>
<def>
<date>1836</date>
<dt>
:any of various large constricting snakes (as a boa)
</dt>
<sd>especially</sd>
<dt>
:any of the large oviparous snakes (subfamily Pythoninae of the family Boidae) of Africa, Asia,
Australia, and adjacent islands that include some of the largest existing snakes
</dt>
</def>
<art>
<bmp>python.bmp</bmp>
<cap>python</cap>
</art>
</entry>
</entry_list>发布于 2015-08-27 15:16:34
所以,我终于想出了解决这个问题的办法。
defn = getattr(entry,'def')https://stackoverflow.com/questions/32242609
复制相似问题