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')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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
  Having a hard time conceptualizing how to print something MysticLord 6 156 Sep-19-2020, 10:43 PM
Last Post: MysticLord
  Having hard time understanding the function self-returning itself twice jagasrik 2 205 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 160 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Having a hard time combining two parts of code. Coozeki 6 558 May-10-2020, 06:50 AM
Last Post: Coozeki
  Program to keep track of reading time test 3 341 Apr-08-2020, 08:38 AM
Last Post: snippsat
  How to clean session mqtt SayHiii 0 386 Dec-09-2019, 07:56 AM
Last Post: SayHiii
  how to clean up unstarted processes? Skaperen 2 371 Aug-27-2019, 05:37 AM
Last Post: Skaperen
  Hard time with prime factors in the task. blackknite 1 563 Jan-23-2019, 04:15 PM
Last Post: ichabod801
  sched.scheduler -> clean denisit 1 962 Nov-28-2018, 09:52 AM
Last Post: Gribouillis
  clean script by code fen1c5 8 1,362 Oct-16-2018, 05:11 AM
Last Post: volcano63

Forum Jump:


Users browsing this thread: 1 Guest(s)