2012-06-14 2 views
0

나는이 오류를 얻을 : 여기은 MySQL의 데이터를 사용하여 파일을 쓸 수 없습니다

TyperError: sequence item 2: expected string, long found 

my code입니다 : 당신이 그것을하고있는 하위 목록으로 길을 문자열을 연결하는 대신

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) 

답변

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])] 
+0

고마워요, 고쳐주세요. –

1

를 사용하여 검색된 데이터를 먼저 문자열로 변환해야합니다그래서 어디서나 row[INDEX]라고 말하면 당신은 말할 필요가 있습니다 : str(row[INDEX]). 예를 들어 str(row[3]).

관련 문제