下面是一个最小的工作示例。我已经用Python 3.4,Python 3.6 32位和Python 3.6 64位进行了测试。lxml - TypeError:write()得到一个意外的关键字参数'default_namespace'
import io
from lxml import etree
test_node = etree.fromstring('''
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
<soap:Body>
<ns1:RequestSecurityToken/>
</soap:Body>
</soap:Envelope>''')
output = io.BytesIO(b'<?xml version="1.0" encoding="UTF-8"?>')
test_node.getroottree().write(output,
encoding="UTF-8",
xml_declaration=None,
default_namespace=None,
method="c14n",
short_empty_elements=False
)
output.seek(0)
print(output.read())
结果:
Traceback (most recent call last):
File "C:/not_telling/c14n.py", line 16, in <module>
short_empty_elements=False
File "lxml.etree.pyx", line 1869, in lxml.etree._ElementTree.write (src\lxml\lxml.etree.c:57004)
TypeError: write() got an unexpected keyword argument 'short_empty_elements'
我刚刚升级LXML版本4.0.0。 (但问题与3.7相同。)
我需要使用C14N方法导出,并且(尽管未包含在本示例中)我还需要指定需要出现在生成的规范窗体中的命名空间列表。例如。我也必须使用inclusive_ns_prefixes参数。
更新:实际上,它似乎是Python内建的xml模块的问题,而不是lxml。
这里是我打电话的方法:
https://github.com/python/cpython/blob/master/Lib/xml/etree/ElementTree.py#L721
它有一个short_empty_elements参数,但它不接受它。
删除参数short_empty_elements? – AK47
[修复3.9](https://github.com/seveas/python-hpilo/commit/02dca196a1e2640d4ecc8985285dc15a7ec27ded) – AK47
我不想删除short_empty_elements参数,因为我需要将它设置为False!而且它似乎是lxml中的一个bug,而不是hpilo(对不起,我已经意外地混淆了这两个项目) – nagylzs