kostasdi kostasdi - 1 year ago 130
MySQL Question

encode the elements of a tuple

i have this code which returns the values of a database using a tuple

# Create a new connection

#create a cursor to the connection

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,))

row = cur.fetchall()
return [((row[0][0]).encode("utf-8"),(row[0][1]),(row[0][2])),]

All the characters in
are greek letters and i must encode them so i will not get as an answer
(u'\u0391\u0393\u03a9\u039d\u0399\u0391', 1978, u'SONY')

(u'ΑΓΩΝΙΑ', 1978, u'SONY')

The problem here is that i dont know the lenght of the tuple.
I know this line of code
return [((row[0][0]).encode("utf-8"),(row[0][1]),(row[0][2])),]
is wrong because it gives me this error
IndexError('tuple index out of range',)

and i am wondering if there is a way to encode all the elements whose are at the
by using something like

Answer Source

You could use map() function. Try

return map( lambda x: (x[0].encode("utf-8"),)+x[1:], row )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download