Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Hard time trying to have clean MySQL to CSV program
#1
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
Quote
#2
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')
Quote
#3
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!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Hard time with prime factors in the task. blackknite 1 354 Jan-23-2019, 04:15 PM
Last Post: ichabod801
  Capitalizing a character at a time program pyth0nus3r 7 1,979 Jan-28-2017, 05:28 PM
Last Post: metulburr
  a 'simple' program, hard as .... to understand meems 3 2,097 Dec-04-2016, 10:59 PM
Last Post: meems

Forum Jump:


Users browsing this thread: 1 Guest(s)