Python Pits..

Q. How many times do we require to convert a python tuple to plain string?
A. It was during one situation that I required to collect some data directly from the database. If this wasent enough, I had to send the data to a mobile using sms. I used MySQLdb and connected to the database.The program was as follows.

import codecs
import MySQLdbconn = MySQLdb.connect(host=’localhost’,use_unicode = True, charset = “utf8″, user=’harshad’,passwd=”,db=’statusnet’)

cursor=conn.cursor()

p=cursor.execute(“””select content from notice where profile_id = 1″””)

g=cursor.fetchall()

q=[]
for i in g:
j=str(i)
q.append(codecs.encode(j))

print len(q)

for e in q:
print codecs.encode(e)

The output of the program was as follows.

python statusnet.py
5
(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,)
(u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,)
(u’Hallo.’,)
(u’Wussup??’,)
(u’!harshad hi..’,)
[“(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,)”, “(u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,)”, “(u’Hallo.’,)”, “(u’Wussup??’,)”, “(u’!harshad hi..’,)”]
root@indiaforce:~# python statusnet.py
5
(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,)
(u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,)
(u’Hallo.’,)
(u’Wussup??’,)
(u’!harshad hi..’,)
[“(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,)”, “(u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,)”, “(u’Hallo.’,)”, “(u’Wussup??’,)”, “(u’!harshad hi..’,)”]

Wow..look at the output. I got a nice tuple. Now do I send a tuple as sms??

After some time I realized that there must be a way to convert all tuples to a string and then send the strings either to the screen or to the mobile or anywhere…

Here is the code..

import MySQLdb
import codecsconn = MySQLdb.connect(host=’localhost’,use_unicode = True, charset = “utf8″, user=’harshad’,passwd=”,db=’statusnet’)

cursor=conn.cursor()

p=cursor.execute(“””select content from notice where profile_id = 1″””)

y=[]
while (1):
a=cursor.fetchone()
if a == None:break
print a
y.append(a)

print y

g=[]
for i in y:
s=i
b=[j.encode(“utf-8”) for j in s]

for i in b:
print b
g.append(i)

print “g > “,g

Output

python statue.py
(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,)
[(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,)]
(u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,)
[(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,), (u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,)]
(u’Hallo.’,)
[(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,), (u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,), (u’Hallo.’,)]
(u’Wussup??’,)
[(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,), (u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,), (u’Hallo.’,), (u’Wussup??’,)]
(u’!harshad hi..’,)
[(u’Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1′,), (u’hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2′,), (u’Hallo.’,), (u’Wussup??’,), (u’!harshad hi..’,)]
[‘Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1’%5D
[‘hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2’%5D
[‘Hallo.’]
[‘Wussup??’]
[‘!harshad hi..’]
g >  [‘Traces, many faces, lost till the end of time… http://localhost/statusnet-0.8.2/index.php/attachment/1’, ‘hello.c http://localhost/statusnet-0.8.2/index.php/attachment/2’, ‘Hallo.’, ‘Wussup??’, ‘!harshad hi..’]

I dont know how many times we land up in a similar situation, now that we have got a list, we can easily iterate through it and get a sleek string as output. No hassles at all.. It still shows some hiccups, nevertheless it works..kludgy but effective. Can work for any amout of rows that we need..

Main intention of writing this place is that I couldent find a better example on google and wondered if I write it, might be useful, and if someone is finding a solution for it, here is it..🙂

End of Logs.

Posted via email from [root@localhost /root]#

PS – In case you wonder I havent added any exception handling code..it is one of my prime mottoes not to write code generating exceptions. so far its been good.😉

print q