【问题标题】:Argument must be bytes or unicode, got '_Element'参数必须是字节或 unicode,得到 '_Element'
【发布时间】:2014-02-17 15:11:15
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x lxml


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2017-05-06
      相关资源
      最近更新 更多