Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何转义特殊字符以使用LXML编写XML_Python_Xml Parsing_Python 2.7_Lxml - Fatal编程技术网

Python 如何转义特殊字符以使用LXML编写XML

Python 如何转义特殊字符以使用LXML编写XML,python,xml-parsing,python-2.7,lxml,Python,Xml Parsing,Python 2.7,Lxml,我有以下带有无效字符的XML <capability_camctrl_privilege> <descr>Indicate whether to support &#8220;Manage Privilege&#8221; <dependent>True</dependent> 但是它不起作用,有人能帮我解决这个问题吗? 多谢各位 Python版本2.7这是一条与lxml有关的常见错误消息。解决方案是在将字符串与lx

我有以下带有无效字符的XML

  <capability_camctrl_privilege>
  <descr>Indicate whether to support &#8220;Manage Privilege&#8221; 
  <dependent>True</dependent>
但是它不起作用,有人能帮我解决这个问题吗? 多谢各位


Python版本2.7

这是一条与
lxml
有关的常见错误消息。解决方案是在将字符串与
lxml
一起使用之前将其转换为unicode。要做到这一点,您需要知道编码,但是如果您碰巧不知道,UTF-8的猜测通常是正确的

in_xml_unicode = unicode(in_xml, 'utf-8')
root = etree.fromstring(in_xml_unicode, parser=etree.XMLParser(recover=True))

您试图准确地包含哪些特殊/无效字符?对James来说,特殊字符可能是“我找到了函数escape(),而quoteattr无法转义符号->”好的。Element()的第一个参数是标记名(即
中的
descr
),它不能包含引号字符。您确定使用的是文档中记录的API吗?我没有将无效字符放在标记名中。无效字符试图放在TEXT.XML_NODE.TEXT中,然后抛出异常和错误消息!是否有函数可以转义LXML中的任何无效字符,例如“:&“;””;也许发布一个导致问题的自足小例子会有所帮助?这可能会让人们更容易提供帮助。
in_xml_unicode = unicode(in_xml, 'utf-8')
root = etree.fromstring(in_xml_unicode, parser=etree.XMLParser(recover=True))