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 in Python by ana1504.k
• 7,890 points
31 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 by SDeb
• 13,210 points

Related Questions In Python

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
111 views
0 votes
1 answer

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

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

answered Apr 17, 2018 in Python by Johnathon
• 9,090 points
51 views
+1 vote
3 answers
0 votes
1 answer

Can someone explain the behaviour of increment and decrement operators in python

down voteaccepted ++ is not an operator. It is ...READ MORE

answered May 15, 2018 in Python by ariaholic
• 7,340 points
489 views
0 votes
1 answer

ssh first with mysqldb in python

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

answered May 23 in Python by ana1504.k
• 7,890 points
745 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
• 57,300 points
4,197 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 in Python by rajesh
• 1,210 points
205 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
• 57,300 points
830 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,210 points
40 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,210 points
154 views