Python Forum

Full Version: Hard time trying to have clean MySQL to CSV program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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')
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!