convert tuple to string

L

Lukas Kasprowicz

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Folks,

My Proglem is, I get after a query on a mysql database with module MySQLdb a
tuple but I need this output from database as a string.
can anybody help?

- -------------- database ---------
import MySQLdb
def select(fields, tables, where):
db=MySQLdb.connect(host=db_host,user=db_user,passwd=db_passwd,db=db_db)
c = db.cursor()
c.execute("SELECT %s FROM %s WHERE %s" %(fields,tables,where))
return c.fetchall()
db.close()


OUTPUT:
- --------------
(('6610@8210',), ('akku',), ('cover',), ('ladekabel',), ('kfz',),
('tischladestation',), ('dummy',), ('Hansytasche',), ('poster',), ('o2',),
('Vodafone',), ('T-Mobile',), ('D1',), ('D2',), ('E+',), ('Eplus',),
('tasche',), ('zubeh\xf6r',), ('Quertasche',), ('Ledertasche',), ('Boom',),
('BELEUCHTUNG',), ('Tastaturmatte',), ('Dummys',), ('Zubeh\xf6rset',),
('TASTATUR',), ('Tastatur',), ('Mittelgeh\xe4use',), ('fast',),
('Displayschutzfolie',), ('Radio',), ('Tischlader',),
('Geh\xe4use\xf6ffner',), ('Oberschale',), ('1 Woche',), ('Alubox',),
('Echtledertasche',), ('E Plus',), ('E+',), ('Eplus',))


greetz Lukas
- --
- ---------------------------------------------------------
Das einzige Mittel gegen Aberglauben ist Wissenschaft.
(Henry Thomas Buckle)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)

iD8DBQE/N5JKHNh65/SR0v0RAmDIAKCUUcd4FuXI9t7g8aU7Mt5nDQaTyQCgoeoN
3yqqMGuLMvWLNgUNDjJk6lY=
=LRtn
-----END PGP SIGNATURE-----
 
A

Alex Martelli

Lukas said:
My Proglem is, I get after a query on a mysql database with module MySQLdb
a tuple but I need this output from database as a string.
can anybody help?

Sure! Just bind that tuple, which you are currently returning, to a
variable (so you can in fact close the connection -- you're not doing
it now, since return ends your function), and then use that tuple as
you prefer. As it's a tuple of tuples you'll probably want to loop
over it rather than just calling "//".join or whatever, of course.

Unless you know how you want to format the resulting string, it's
unlikely that the result is going to be satisfactory to you, of course.


Alex
 
A

Alex Martelli

Anand said:
Assuming 't' is your tuple of values,

print reduce(lambda x, y: x + ',' + y, map(lambda x: x[0], t))

will print a string with all first elements of the tuple
(your strings), separated by a comma.

So will ','.join([ x[0] for x in t ]) .

So, let's look at performance. The complex, lambda-rich
expression with a map and a reduce...:

python2.3 timeit.py -s't=tuple([ (str(x),) for x in range(30) ])'
'reduce(lambda x,y:x+","+y, map(lambda x:x[0],t))'

10000 loops, best of 3: 59.3 usec per loop


The simple expression based on a list comprehension:

python2.3 timeit.py -s't=tuple([ (str(x),) for x in range(30) ])'
'",".join([ x[0] for x in t ])'

10000 loops, best of 3: 24.4 usec per loop


I think this is great fodder for the underground movement aiming
to remove lambda, map and reduce. All that complication just
to slow things down by two times and a half...?-!


Alex
 
J

Jack Diederich

So, let's look at performance. The complex, lambda-rich
expression with a map and a reduce...:

python2.3 timeit.py -s't=tuple([ (str(x),) for x in range(30) ])'
'reduce(lambda x,y:x+","+y, map(lambda x:x[0],t))'

10000 loops, best of 3: 59.3 usec per loop

The simple expression based on a list comprehension:

python2.3 timeit.py -s't=tuple([ (str(x),) for x in range(30) ])'
'",".join([ x[0] for x in t ])'

10000 loops, best of 3: 24.4 usec per loop

I think this is great fodder for the underground movement aiming
to remove lambda, map and reduce. All that complication just
to slow things down by two times and a half...?-!

No, this is a call for lambda/map/filter fans to improve the
intepreter. The above reduce has 2*30 more function calls
per loop than the equivalent list comprehension. The list comp
essentially gets inlined, the lambda gets called and the
arguments parsed each time.

A truer comparison would also have the top one doing ",".join or
the bottom one using reduce. Having one doing 30*3 string concats
and the other doing one join() will skew the results.

-jackdied
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top