0

我收到此错误:

TyperError: sequence item 2: expected string, long found

这是我的代码

import MySQLdb
connection = MySQLdb.connect (host = "localhost", user = "root", passwd = "*****", db = "test")
cursor = connection.cursor ()
cursor.execute ("SELECT * FROM names")
data = cursor.fetchall ()
no = []
no += ["<users>\n"]
for row in data :
        no += ["        <user id='", row[0], "' name='", row[1], "' blend='", row[2], "'/>\n"]
        no += ["                <nick color='0x", row[3], "' font='", row[4], "'/>\n"]
        no += ["                <glow color='0x", row[5], "' alpha='", row[6], "' blurX='", row[7], "' blurY='", row[8], "' strength='", row[9], "' quality='", row[10], "'/>\n"]
        no += ["        </user>\n", row[11]]
cursor.close ()
connection.close ()
no += ["</users>"]
s = ''.join(no)
file = open('test.xml','w')
file.write(s)
4

2 回答 2

1

您可以执行以下操作,而不是按照您的方式将字符串连接到子列表中

# Don't do this
no += ["        <user id='", row[0], "' name='", row[1], "' blend='", row[2], "'/>\n"]

# Do this instead
no += ["        <user id='%s' name='%s' blend='%s'/>\n" % (row[0], row[1], row[2])]
于 2012-06-14T04:15:48.050 回答
1

您必须首先将检索到的数据转换为字符串,以便在它说您必须说的str()任何地方: . 例如。row[INDEX]str(row[INDEX])str(row[3])

于 2012-06-14T04:15:50.773 回答