Python ElementTree“找不到元素”异常
16lz
2021-01-22
Good day everybody.
大家好日子。
I'm trying to read, parse and use an xml-file using ElementTree. Following data:
我正在尝试使用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 />
Sadly, I ElementTree gives an exception!!! Reading the file with following code:
可悲的是,我的ElementTree给了一个例外!!!使用以下代码读取文件:
import xml.etree.ElementTree as ET
***code***
tree = ET.parse("res\\data.xml")
root = tree.getroot()
Exception:
例外:
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***
I can't figure out what's wrong, I've tried to change data.xml in every possible way I can imagine, no difference. It's always the last line of the file! What am I doing wrong? Thanks!
我无法弄清楚出了什么问题,我试图以我能想象的每一种可能的方式改变data.xml,没有区别。它始终是文件的最后一行!我究竟做错了什么?谢谢!
2 个解决方案
#1
4
Your tags are not closed properly. For example, to close a "meteorite" tag, use </meteorite>
not <meteorite />
.
您的代码未正确关闭。例如,要关闭“陨石”标签,请使用
而不是
更多相关文章
- python 处理csv文件的过程对换行符的处理
- linux修改文件所属用户和组
- 八大经典排序算法基本思想及代码实现(插入排序,希尔排序,选择排序,
- 贝叶斯学习 -- matlab、python代码分析(3)
- Linux的文件权限
- linux系统更改目录和文件的权限总结
- UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的sh
- Linux下objdump查看C程序编译后的汇编代码
- CentOS7.2 通过nfs设置共享文件夹