下面是一個最小的工作示例。我已經用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