473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 18151
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alastair G. Hogge | last post by:
Hello *, I'm using Python and the cgi module to retrive data from a HTML form. I'm then trying to get that information into a string. But efforts fail when I run type(foo) on my string. type()...
1
by: Michal Mikolajczyk | last post by:
Is there a quick way to convert a unicode tuple to a tuple containing python strings? (u'USER', u'NODE', u'HASH', u'IDNBR') to this: ('USER', 'NODE', 'HASH', 'IDNBR') I need to be able...
1
by: Jinming Xu | last post by:
Hello everyone, While embedding my C++ program with Python, I am impeded by the conversion from a Python Tuple to a C++ array. I hope to get some assistance from you guys. I have a sequence...
16
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
7
by: querypk | last post by:
how do I convert b is a string b = '(1,2,3,4)' to b = (1,2,3,4)
1
by: Bell, Kevin | last post by:
I'm pulling a range of cells from Excel into a list and the data in Excel is a number or possibly some text like an asterisk. Each member of the list is a com object (I think) and I'm converting...
2
by: aqmaiya | last post by:
Hello, there is string value 'Dec 06, 2000' I want to convert that string date to SHORT FORMAT like '2000-12-06-. Please help me how do I do that? I'm new in Jython. Thanks, aqmaiya
8
by: hidrkannan | last post by:
I am reading a set of data from the excel workbook. Data are contained in 2 columns. Column A contains the names and Column B contains values. The issue is the value can be of any type integer,...
0
by: dudeja.rajat | last post by:
On Tue, Aug 19, 2008 at 12:40 PM, <dudeja.rajat@gmail.comwrote: Googled and found : s = "v%d.%d.%d.%d" % tuple(version) print s it's working. Result is : v1.2.3.4
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.