0
我有一个很大的XML文件(200万对象的详细信息),其内容类似于下图所示。文件大小是657MBcElementTree.ParseError:格式不正确(无效令牌)
<?xml version="1.0" encoding="UTF-8?>
<root>
<item>
<rank>1</rank>
<landinglink>www.google.com</landinglink>
<descrip>some text</descrip>
</item>
<item>
<rank>1</rank>
<landinglink>www.facebook.com</landinglink>
<descrip>some text</descrip>
</item>
<item>
<rank>1</rank>
<landinglink>www.xyz.com</landinglink>
<descrip>some text</descrip>
</item>
.
.
.
.
.
.
.
</root>
我试图打印所有的'连接'。我正在使用的代码如下所示。
import xml.etree.cElementTree as ET
for event, elem in ET.iterparse("filename.xml"):
if event == 'end' and elem.tag == 'item':
print elem.find('landinglink').text
但是,当执行代码时,它给了我下面的错误。
Traceback (most recent call last):
File "D:/test.py", line 2, in <module>
for event, elem in ET.iterparse("filename.xml"):
File "<string>", line 91, in next
cElementTree.ParseError: not well-formed (invalid token): line 1338, column 298
此错误在不同位置持续重复。如何避免这种类型的错误。任何帮助将不胜感激。
那条线上,那个位置是什么? – cco
一些文字说明,但它包含xA0种类的一些错误 –
如果该值为'\ xA0',那么你的文件没有正确编码为utf-8。 – cco