Python Forum
Hard time trying to have clean MySQL to CSV program
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
Reply
#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

Reply
#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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can i clean this code ? BSDevo 8 849 Oct-28-2023, 05:50 PM
Last Post: BSDevo
  Mysql and mysql.connector error lostintime 2 611 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Hard time trying to figure out the difference between two strings carecavoador 2 644 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Clean Up Script rotw121 2 981 May-25-2022, 03:24 PM
Last Post: rotw121
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,676 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  How to clean UART string Joni_Engr 4 2,411 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  First time with mysql pascal111 5 3,454 Sep-25-2021, 07:45 AM
Last Post: Pedroski55
  Program stuck at mysql.connector.connect zazas321 1 2,039 Jul-29-2021, 10:49 AM
Last Post: zazas321
  Having a hard time conceptualizing how to print something MysticLord 6 3,041 Sep-19-2020, 10:43 PM
Last Post: MysticLord
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,449 Aug-15-2020, 08:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020