[英]encode the elements of a tuple
我有這段代碼使用元組返回數據庫的值
# Create a new connection
con=connection()
#create a cursor to the connection
cur=con.cursor()
cur.execute("SELECT tragoudi.titlos, tragoudi.etos_par, cd_production.etaireia FROM tragoudi JOIN singer_prod ON tragoudi.titlos=singer_prod.title JOIN cd_production ON singer_prod.cd=cd_production.code_cd GROUP BY tragoudi.titlos HAVING tragoudi.titlos LIKE %s AND tragoudi.etos_par LIKE %s AND cd_production.etaireia LIKE %s",(titlos,etos_par,etaireia,))
con.commit()
row = cur.fetchall()
return [((row[0][0]).encode("utf-8"),(row[0][1]),(row[0][2])),]
row[:][0]
中的所有字符都是希臘字母,我必須對其進行編碼,因此我不會得到答復(u'\Α\Γ\Ω\Ν\Ι\Α', 1978, u'SONY')
但是(u'ΑΓΩΝΙΑ', 1978, u'SONY')
這里的問題是我不知道元組的長度。 我知道這行代碼return [((row[0][0]).encode("utf-8"),(row[0][1]),(row[0][2])),]
是錯誤的,因為它給了我這個錯誤IndexError('tuple index out of range',)
我想知道是否有一種方法可以通過使用諸如row[:][0].encode("utf-8")
的方法來對所有在row[something][0]
的元素進行編碼
您可以使用map()
函數。 嘗試
return map( lambda x: (x[0].encode("utf-8"),)+x[1:], row )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.