Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert tuple to string

Reply
Thread Tools

convert tuple to string

 
 
Lukas Kasprowicz
Guest
Posts: n/a
 
      08-11-2003
-----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-----
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      08-11-2003
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

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      08-11-2003
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+","+y, map(lambda 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

 
Reply With Quote
 
Jack Diederich
Guest
Posts: n/a
 
      08-11-2003
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+","+y, map(lambda 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c")) Steve Python 1 12-13-2005 10:25 PM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57