convert tuple to string

Discussion in 'Python' started by Lukas Kasprowicz, Aug 11, 2003.

  1. -----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-----
     
    Lukas Kasprowicz, Aug 11, 2003
    #1
    1. Advertising

  2. 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
     
    Alex Martelli, Aug 11, 2003
    #2
    1. Advertising

  3. 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
     
    Alex Martelli, Aug 11, 2003
    #3
  4. 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
     
    Jack Diederich, Aug 11, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    960
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    615
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    826
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    970
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page