0

I have a mysql database that looks like this:

ID | Name
----------
1  | Vårin
2  | Nils
3  | Jørgen

When i run my code:

#!/usr/bin/python

include mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="user",
    passwd="passwd",
    database="database",
    charset='utf8',
    use_unicode=True
    )

mycursor.execute("SELECT * FROM table")

results = mycursor.fetchall()

for n in results:
    print n

it prints (u'1', u'V\xe5rin'), (u'2', u'Nils'), (u'3', u'J\xf8rgen')

How do I get it to print ('1', 'Vårin'), ('2', 'Nils'), ('3', 'Jørgen') ?

Edit: Using python 2.7

Karset
  • 5
  • 2
  • Where are you printing to? The values themselves are fine, likely your console just doesn't handle UTF-8 correctly or such. – deceze Apr 23 '20 at 11:31
  • 2
    Are you using `Python 2.7`? By default Strings in that version are `bytes` and a string that has the prefix `u` is an indicator of a unicode-string. Printing the string-itself will return the string in the correct format. – Hampus Larsson Apr 23 '20 at 11:31
  • As @HampusLarsson told, Python3 directly shows the output with simple print – rohan goli Apr 23 '20 at 11:42
  • Looks like you stored latin1-encoded text, then expected to display it as utf8. More tips: https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored and http://mysql.rjweb.org/doc.php/charcoll#python – Rick James Apr 25 '20 at 22:51

0 Answers0