Python:lxml.etree.tostring(with_comments=False) [英] Python: lxml.etree.tostring(with_comments=False)
问题描述
我调用以下命令并收到以下错误:
I call the following command and get the following error:
>>>lxml.etree.tostring([tree].getroot(), with_comments=False)
ValueError: Can only discard comments in C14N serialisation
我不知道 C14N 是什么,但我希望能够解释如何实现它并使用 with_comments=False
运行上述命令.(是的,我知道我可以使用正则表达式去除评论.请不要提供正则表达式作为解决方案.)
I don't know what C14N is, but I would appreciate an explanation of how I can achieve it and run the foregoing command with with_comments=False
. (Yes, I'm aware that I can strip the comments using regex. Please don't offer regular expressions as a solution.)
背景:我想通过 http 连接传输我的 xml 文档.我正在使用 lxml Python 库.我在 Python 2.7.1 上运行
Background: I want to transfer my xml doc over an http connection. I'm using the lxml Python library. I'm running on Python 2.7.1
推荐答案
lxml.etree.tostring 文档说:
exclusive 和 with_comments 参数仅用于 C14N 输出,它们分别请求独占和未注释的 C14N 序列化.
The exclusive and with_comments arguments are only used with C14N output, where they request exclusive and uncommented C14N serialisation respectively.
该参数仅在使用 method='c14n'
时有效.你可以省略它,据我所知,它不会包括评论.即使是这样,接收端的 xml 解析器也应该忽略它们,所以除非有带宽问题或您有特定问题,否则我不会担心.
That parameter is only valid when using method='c14n'
. You can omit it, and as far as I know, it will not include comments. Even if it did, the xml parser on the receiving end should ignore them, so unless there's a bandwidth concern or you have a specific problem with it, I wouldn't worry about it.
这篇关于Python:lxml.etree.tostring(with_comments=False)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!