参数必须是字节或Unicode,为'_Element' [英] Argument must be bytes or unicode, got '_Element'

查看:127
本文介绍了参数必须是字节或Unicode,为'_Element'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.3并遇到此错误

from lxml import etree
xmlns = "http://www.fpml.org/FpML-5/confirmation"
xsi  = "http://www.w3.org/2001/XMLSchema-instance"
fpmlVersion="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-6.xsd http://www.w3.org/2000/09/xmldsig# ../../xmldsig-core-schema.xsd"
page = etree.Element("{"+xmlns+"}dataDocument",nsmap={None:xmlns,'xsi':xsi })
page.set("fpmlVersion", fpmlVersion)
doc = etree.SubElement(page,trade)
s = etree.tostring(doc, xml_declaration=True,encoding="UTF-8",pretty_print=True)
print (s)

TypeError: Argument must be bytes or unicode, got '_Element'

我希望输出为

<dataDocument xmlns="http://www.fpml.org/FpML-5/confirmation" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
fpmlVersion="http://www.tradfpml.org/FpML-5/confirmation ../../fpml-main-5-6.xsd http://www.w3.org/2000/09/xmldsig# ../../xmldsig-core-schema.xsd">
<trade>
</trade>
</dataDocument>

我认为SubElement()函数的第二个参数必须是字符串,并且python对此有所抱怨.应该是:

doc = etree.SubElement(page,"trade")

I am using Python 3.3 and running into this error

from lxml import etree
xmlns = "http://www.fpml.org/FpML-5/confirmation"
xsi  = "http://www.w3.org/2001/XMLSchema-instance"
fpmlVersion="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-6.xsd http://www.w3.org/2000/09/xmldsig# ../../xmldsig-core-schema.xsd"
page = etree.Element("{"+xmlns+"}dataDocument",nsmap={None:xmlns,'xsi':xsi })
page.set("fpmlVersion", fpmlVersion)
doc = etree.SubElement(page,trade)
s = etree.tostring(doc, xml_declaration=True,encoding="UTF-8",pretty_print=True)
print (s)

TypeError: Argument must be bytes or unicode, got '_Element'

I want the output to be

<dataDocument xmlns="http://www.fpml.org/FpML-5/confirmation" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
fpmlVersion="http://www.tradfpml.org/FpML-5/confirmation ../../fpml-main-5-6.xsd http://www.w3.org/2000/09/xmldsig# ../../xmldsig-core-schema.xsd">
<trade>
</trade>
</dataDocument>

解决方案

I think that second argument to SubElement() function has to be a string, and python complaints about it. It should be:

doc = etree.SubElement(page,"trade")

这篇关于参数必须是字节或Unicode,为'_Element'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆