ParseError:使用cElementTree格式不正确(无效令牌) [英] ParseError: not well-formed (invalid token) using cElementTree

查看:223
本文介绍了ParseError:使用cElementTree格式不正确(无效令牌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部来源接收到xml字符串,其中可能包含未经消毒的用户提供的内容.

I receive xml strings from an external source that can contains unsanitized user contributed content.

以下xml字符串在cElementTree中给出了ParseError:

The following xml string gave a ParseError in cElementTree:

>>> print repr(s)
'<Comment>dddddddd\x08\x08\x08\x08\x08\x08_____</Comment>'
>>> import xml.etree.cElementTree as ET
>>> ET.XML(s)

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    ET.XML(s)
  File "<string>", line 106, in XML
ParseError: not well-formed (invalid token): line 1, column 17

有没有办法让cElementTree不抱怨?

Is there a way to make cElementTree not complain?

推荐答案

似乎在抱怨\x08,您需要对此进行转义.

It seems to complain about \x08 you will need to escape that.

或者您可以让解析器使用recover

Or you can have the parser ignore the errors using recover

from lxml import etree
parser = etree.XMLParser(recover=True)
etree.fromstring(xmlstring, parser=parser)

这篇关于ParseError:使用cElementTree格式不正确(无效令牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆