参数必须是bytes或unicode,得到'\u Element'

2024-05-23 17:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3.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>

Tags: orghttpwwwpageetreexsdconfirmationxmlns