Python mysqldb and unicode

0 votes

I'm having trouble to get unicode values out of mysql queries.

I am working on the following code:

>>> from MySQLdb import connect
>>> from MySQLdb.cursors import DictCursor
>>> con = connect(
    passwd = "****",
    db = 'my_db',
    user = "db_user",
    host = "localhost",
    cursorclass = DictCursor,
    use_unicode=True,
    charset="utf8"
)
>>> cursor = con.cursor ()
>>> cursor.execute (u'Select * from basic_applet')
>>> res = cursor.fetchall()
>>> print(res)
({'Title_de': 'test title', .... })
>>> type(res[0]['Title_de'])
<type 'str'>

But it is not returning unicode.

In my table structure, Title_de is set as unicode.

IDbasic_applet  int(10)...
Title_de    varchar(75)     utf8_bin

I am not able to figure out the problem. Can anyone please tell me how to fix this?

Aug 1, 2019 in Python by ana1504.k
• 7,910 points
1,775 views

1 answer to this question.

0 votes

What you get is a bytestring. You must decode it in order to get a unicode string. It basically comes down to this:

>>> byte_string = 'F\xe9vrier'
>>> byte_string.decode('UTF-8')
u'Février'
answered Aug 1, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
1 answer

How to use MySQLdb with Python and Django?

Hello @kartik, I had the same error and pip ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
804 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
2,343 views
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
859 views
+1 vote
7 answers
+1 vote
1 answer

ssh first with mysqldb in python

Setup an ssh tunnel before you use ...READ MORE

answered May 23, 2019 in Python by ana1504.k
• 7,910 points
2,788 views
0 votes
1 answer

ProgrammingError: not all arguments converted during string formatting

Sorted!!!! just found the solution, 1 - apparently ...READ MORE

answered Sep 10, 2018 in Python by Priyaj
• 58,020 points
20,289 views
0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,270 points
1,892 views
0 votes
1 answer

ProgrammingError: not all arguments converted during string formatting

Sorted!!!! just found the solution, 1 - apparently ...READ MORE

answered Sep 20, 2018 in Python by Priyaj
• 58,020 points
5,697 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,300 points
928 views
0 votes
1 answer

Python constructors and __init__

There is no function overloading in Python, ...READ MORE

answered Oct 11, 2018 in Python by SDeb
• 13,300 points
1,116 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP