Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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-TypeError:write()获取了意外的关键字参数';默认名称空间';_Python_Xml_Lxml_C14n - Fatal编程技术网

Python lxml-TypeError:write()获取了意外的关键字参数';默认名称空间';

Python lxml-TypeError:write()获取了意外的关键字参数';默认名称空间';,python,xml,lxml,c14n,Python,Xml,Lxml,C14n,下面是一个简单的工作示例。我已经用Python3.4、Python3.6 32位和Python3.6 64位进行了测试 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-tru

下面是一个简单的工作示例。我已经用Python3.4、Python3.6 32位和Python3.6 64位进行了测试

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())
导入io
从lxml导入etree
test_node=etree.fromstring(“”)
''')
输出=io.BytesIO(b'')
test_node.getroottree().write(输出、,
encoding=“UTF-8”,
xml_声明=无,
默认名称空间=无,
method=“c14n”,
短\u空\u元素=假
)
输出搜索(0)
打印(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'
回溯(最近一次呼叫最后一次):
文件“C:/not_telling/c14n.py”,第16行,在
短\u空\u元素=假
lxml.etree.\u ElementTree.write(src\lxml\lxml.etree.c:57004)中第1869行的文件“lxml.etree.pyx”
TypeError:write()获得意外的关键字参数“short\u empty\u elements”
我刚刚将lxml版本升级到4.0.0。(但3.7的问题也是如此。)

我需要使用C14N方法导出,并且(尽管示例中没有包括)我还需要指定需要以生成的规范形式出现的名称空间列表。例如,我还必须使用inclusive_ns_prefixes参数

更新:实际上,Python的内置xml模块似乎有问题,而不是lxml

下面是我调用的方法:


它确实有一个short\u empty\u elements参数,但它不接受它。

lxml中的
\u ElementTree.write()
方法不支持
默认\u名称空间
short\u empty\u elements
参数。看


但是,自Python 3.4以来,这两个参数都在
ElementTree
标准模块中可用。请参阅。

删除参数short\u empty\u elements?我不想删除short\u empty\u elements参数,因为我需要将其设置为False!这似乎是lxml中的一个bug,而不是hpilo中的bug(很抱歉,我意外地混淆了这两个项目),成功地混淆了不是两个,而是三个项目。有史以来最糟糕的问题。。。找到了方法,更新了问题。实际上不仅仅是
short\u empty\u elements
,elementTree write方法似乎讨厌在数据
输出
本身之后出现的任何参数、位置或关键字。是的,但我还需要包含特定的名称空间。虽然问题中没有包含它,但我还必须传递exclusive=True和c14n的名称空间前缀列表。标准elementtree不支持这些,因此我无法使用它。更新了问题,因此现在很清楚我必须坚持使用lxml,因为c14n方法和inclusive_ns_前缀仅在那里可用。问题的标题是“lxml-TypeError:write()获得了意外的关键字参数'default_namespace'。我已经解释了你为什么会犯那个错误。很抱歉,这并没有真正的帮助,但这正是你所问的。好吧,我接受你的回答,也许会发布一个新的问题。显然,提出了错误的问题。