2017-09-22 93 views
2

下面是一個最小的工作示例。我已經用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參數,但它不接受它。

+0

刪除參數short_empty_elements? – AK47

+0

[修復3.9](https://github.com/seveas/python-hpilo/commit/02dca196a1e2640d4ecc8985285dc15a7ec27ded) – AK47

+0

我不想刪除short_empty_elements參數,因爲我需要將它設置爲False!而且它似乎是lxml中的一個bug,而不是hpilo(對不起,我已經意外地混淆了這兩個項目) – nagylzs

回答

1

在lxml中_ElementTree.write()方法不支持參數default_namespaceshort_empty_elements。見http://lxml.de/api/lxml.etree._ElementTree-class.html#write

但是,自從Python 3.4以來,這兩個參數在ElementTree標準模塊中都可用。見https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write

+0

是的,但我也需要包含特定的命名空間。雖然它沒有包含在問題中,但我還必須爲c14n傳遞exclusive = True和一個名稱空間前綴列表。標準elementtree不支持這些,所以我不能使用它。 – nagylzs

+0

更新了問題,所以現在清楚我必須堅持使用lxml,因爲c14n方法和inclusive_ns_prefixes僅在那裏可用。 – nagylzs

+0

問題的標題是「lxml - TypeError:write()有一個意外的關鍵字參數'default_namespace'」。我已經解釋了爲什麼你會得到這個錯誤。我很抱歉它沒有真正幫助,但這是你問的。 – mzjn

相關問題