gpt4 book ai didi

python - 导入错误 : No module named elementtree. SimpleXMLWriter

转载 作者:数据小太阳 更新时间:2023-10-29 02:08:51 25 4
gpt4 key购买 nike

在我的 python 代码中,我试图以 XML 格式显示输出。为此,我使用了 XMLwriter

但是显示错误:

Traceback (most recent call last):
File "C:\Users\Ponmani\Desktop\test.cgi", line 8, in <module>
from elementtree.SimpleXMLWriter import XMLWriter
ImportError: No module named elementtree.SimpleXMLWriter

导致错误的行是:

from elementtree.SimpleXMLWriter import XMLWriter

我的整个 python 代码是:

import os
import cgi
import MySQLdb
import cgitb
from xml.etree.ElementTree import ElementTree
from elementtree.SimpleXMLWriter import XMLWriter
import sys
import SecureDb
cgitb.enable()
print "Content-type: text/xml\n\n";
root=xml.start("root")
conn= MySQLdb.connect(host = SecureDb.host ,user =SecureDb.user ,passwd=SecureDb.password ,db=SecureDb.database)
cursor=conn.cursor()
xml=XMLWriter(sys.stdout)
cursor.execute("select * from register where Name='Subburaj'")
result=cursor.fetchall()
if(result!=()):
for colns in result:
xml.start("Group")
xml.element("Name","%s" %(colns[0]))
xml.element("Mail","%s" %(colns[1]))
print result
xml.end()
xml.close(root)
conn.commit()
cursor.close()
conn.close()

最佳答案

Python 2.5 及更高版本附带的 ElementTree 模块包含 SimpleXMLWriter 模块;后者完全独立于 ElementTree 功能的其余部分。

为了生成 XML,我个人使用模板语言,例如 Chameleon .您还可以使用 ElementTree API 本身构建树,然后只需对结果调用 .write()

关于python - 导入错误 : No module named elementtree. SimpleXMLWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com