469,358 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

convert tuple to string

-----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,passw d=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-----
Jul 18 '05 #1
3 17848
Lukas Kasprowicz wrote:
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

Jul 18 '05 #2
Anand Pillai wrote:
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

Jul 18 '05 #3
On Mon, Aug 11, 2003 at 08:40:13PM +0000, Alex Martelli wrote:

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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alastair G. Hogge | last post: by
1 post views Thread by Michal Mikolajczyk | last post: by
1 post views Thread by Jinming Xu | last post: by
16 posts views Thread by flyaflya | last post: by
7 posts views Thread by querypk | last post: by
1 post views Thread by Bell, Kevin | last post: by
2 posts views Thread by aqmaiya | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.