Python Forum
Hard time trying to have clean MySQL to CSV program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Hard time trying to have clean MySQL to CSV program (/thread-11663.html)



Hard time trying to have clean MySQL to CSV program - PierreSoulier - Jul-20-2018

Hello,

I want to know how to get 'clean' results on the MySQL querry using python.

Here is the program:

 cur.execute('select nom_region from data where dept_epci like %s and nom_commune like %s ', [test, x[5]])
 region = cur.fetchall()    
 print (region)
And here is the result
(('Occitanie',),)
I just want that program to print (or return) only Occitanie.

Do you have any idea how i could manage to do so?

Thank you


RE: Hard time trying to have clean MySQL to CSV program - buran - Jul-20-2018

currsor.fetchall() will return tuple of tuples. Given your query it will be tuple of single-element tuples.
loop over the result to get what you want, e.g.
regions = cur.fetchall()
if regions:
    regions = [item[0] for item in regions]
    print(', '.join(regions))
else:
    print('No matching records')



RE: Hard time trying to have clean MySQL to CSV program - PierreSoulier - Jul-20-2018

Thank you ! :)

I've tried something like this:

#        region = str(region[0])
#        region = region.replace('(','')
#        region = region.replace(')','')
#        region = region.replace('\'','')
#        region = region.replace(',','')
but it's kinda uneficient.

Thank you!