Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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.etree.tostring(带有\u注释=False)_Python_Xml_Xml Serialization - Fatal编程技术网

Python:lxml.etree.tostring(带有\u注释=False)

Python:lxml.etree.tostring(带有\u注释=False),python,xml,xml-serialization,Python,Xml,Xml Serialization,我调用以下命令并得到以下错误: >>>lxml.etree.tostring([tree].getroot(), with_comments=False) ValueError: Can only discard comments in C14N serialisation 我不知道C14N是什么,但我希望您能解释一下如何实现它,并使用和\u comments=False运行前面的命令。(是的,我知道我可以使用正则表达式去除注释。请不要提供正则表达式作为解决方案。) 背景:我

我调用以下命令并得到以下错误:

>>>lxml.etree.tostring([tree].getroot(), with_comments=False)
ValueError: Can only discard comments in C14N serialisation
我不知道C14N是什么,但我希望您能解释一下如何实现它,并使用
和\u comments=False运行前面的命令。(是的,我知道我可以使用正则表达式去除注释。请不要提供正则表达式作为解决方案。)

背景:我想通过http连接传输我的xml文档。我正在使用lxml Python库。我正在运行Python 2.7.1,文档上说:

exclusive和with_comments参数仅用于C14N输出,它们分别请求独占和未注释的C14N序列化


该参数仅在使用
方法='c14n'
时有效。你可以省略它,据我所知,它不会包括评论。即使是这样,接收端的xml解析器也应该忽略它们,因此,除非存在带宽问题或您对此有特定问题,否则我不会担心。您可以在解析时删除注释:

parser = etree.XMLParser(remove_comments=True)
tree = etree.parse(xmlfile, parser=parser)
或者在使用objectify时(我花了很长时间才发现):


这是什么版本的lxml?“C14N”是“规范化”的缩写,它指的是规范化的XML序列化:对,它是
tostring()
。(不过,我在标题中说对了)。我无法说出lxml的哪个版本(但我假设这个问题是因为我写错了方法名而引起的)。
parser = objectify.makeparser(remove_comments=True)
tree = objectify.parse(xmlfile, parser=parser)