首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数必须是字节或unicode,获得'_Element‘。

参数必须是字节或unicode,获得'_Element‘。
EN

Stack Overflow用户
提问于 2014-02-17 15:11:15
回答 1查看 11K关注 0票数 2

我使用Python3.3并遇到了这个错误

代码语言:javascript
复制
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)
代码语言:javascript
复制
TypeError: Argument must be bytes or unicode, got '_Element'

我希望输出是

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-17 15:22:43

我认为SubElement()函数的第二个参数必须是字符串,而python则抱怨它。它应该是:

代码语言:javascript
复制
doc = etree.SubElement(page,"trade")
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21832584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档