Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Convert tuples into string

Reply
Thread Tools

Re: Convert tuples into string

 
 
Tim Chase
Guest
Posts: n/a
 
      11-26-2012
On 11/25/12 20:24, Smaran Harihar wrote:
> I was able to solve it using the following loop,
>
> conn=psycopg2.connect(connstr)
> cursor=conn.cursor()
> cursor.execute("SELECT tablename FROM pg_tables where tablename like '%"+
> inp +"%'")
> records = cursor.fetchall()
> str=""
> for rec in records:
> if str == "":
> str="".join(rec)
> else:
> m="".join(rec)
> str=str+","+m


Sounds like you want the first element of the tuple which you can
access either as "row[0]" or by unpacking the tuple into a
one-element tuple. The first would look something like

cursor.execute(...)
s = ",".join(rec[0] for rec in cursor.fetchall())

while the second would look something like

cursor.execute(...)
s = ",".join(tablename for (tablename,) in cursor.fetchall())

Note that I'm directly building the string with .join rather than
appending (the growth of which has bad performance in Python).

-tkc


 
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
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Re: Convert tuples into string Cameron Simpson Python 0 11-26-2012 02:21 AM
Passing tuples of tuples to sqlite xera121 Python 8 09-30-2009 05:45 AM
tuples within tuples korovev76@gmail.com Python 12 10-27-2007 08:16 PM
Different tuples to one container? (One type of a pointer to point to different kinds of tuples?) fff_afafaf@yahoo.com C++ 5 10-05-2006 11:17 PM



Advertisments