大家好日子。Python ElementTree“找不到元素”异常
我想读取,解析和使用ElementTree的XML文件。 以下数据:
<level>
<leveldata>
<level name="hh" difficulty="Easy" lenght="3600">
<meteorite chance="4" speed="3" >
<image id="1">
<image id="2">
<image id="3">
<meteorite />
<meteorite chance="4" speed="3" >
<image id="4">
<image id="5">
<image id="6">
<meteorite />
<level />
<leveldata />
<meteorimages>
<meteor id="5" imagepath="res\meteorit_1.png">
<meteor id="5" imagepath="res\meteorit_2.png">
<meteor id="5" imagepath="res\meteorit_3.png">
<meteorimages />
<datasheet />
<level />
可悲的是,我的ElementTree给出了一个例外!读取该文件与下面的代码:
import xml.etree.ElementTree as ET
***code***
tree = ET.parse("res\\data.xml")
root = tree.getroot()
例外:
File "E:\blabla\core.py", line 26, in load_levelproperties
*tree = ET.parse("res\\data.xml")* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
1182, in parse
*tree.parse(source, parser)* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
657, in parse
*self._root = parser.close()* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
1654, in close
*self._raiseerror(v)* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
1506, in _raiseerror
***raise err xml.etree.ElementTree.ParseError: no element found: line 16, column 9***
我无法弄清楚什么是错的,我试图改变data.xml中百般我可以想像,没有区别。它始终是文件的最后一行! 我在做什么错? 谢谢!
看起来我今天在欢迎派对团队!欢迎来到Stack Overflow @grvsmth。恭喜你的博士学位,我希望你能'while(yourStay){try {StackOverflow} catch {enjoyment}}'。是的,这很古怪,但嘿,幽默是我们人类仍然可以比电脑做得更好的最后一件事情之一。 –
恩,谢谢。 这就是我需要的。 – Nearoo
很高兴帮助,并感谢您的欢迎! – grvsmth