Skip to content Skip to sidebar Skip to footer

Python Change Character Encoding To Utf_8

actual data:CN=username,OU=CompanyName,DC=company,DC=intra(how it seems in MySQL db) and when I fetch this data, this is how it seems in python variable(retrieved from MySQL): CN=u

Solution 1:

Can you check encoding by following method:

>>>import sys>>>sys.getdefaultencoding()
'utf-8'
>>>

If encoding is ascii then set to utf-8

  1. open following file(I am using Python 2.7):

    /usr/lib/python2.7/sitecustomize.py

  2. then update following to utf-8

    sys.setdefaultencoding("utf-8")

[Edit 2]

Can you add following in tour code(at start) and then check:-

>>>try:...import apport_python_hook...except ImportError:...pass...else:...    apport_python_hook.install()...>>>import sys>>>>>>sys.setdefaultencoding("utf-8")>>>>>>

Solution 2:

This error means that your message is already a unicode object, no decoding needed.

When you are doing:

truestr = unicode(string, 'utf-8')

your variable string is first implicitly converted to str type using default 'ascii' codec. And of course, it fails because your string contains non-ascii characters.

If you want to write string somewhere as UTF-8, use string.encode('utf-8').


Note: I've renamed your str variable to string because of name clash with built-in str type. Naming variable str (or int, or float, etc.) is a very bad style.

Solution 3:

go to this file

vi /usr/lib/python2.7/site-packages/sitecustomize.py

Add this text

import sys

reload(sys)

sys.setdefaultencoding("utf-8")

Solution 4:

Default encoding of your system is ASCII. use "sys.setdefaultencoding" to switch it to utf-8 encoding. This function is only available on startup while python scans the environment. To use this function you have to reload sys after importing the module. Following is the code for you problem.

import sys
reload(sys)
sys.setdefaultencoding ("utf-8")

Edit:

If you want to use utf-8 encoding than use it at the very beginning of your code. If you use it in middle of your code than it will create problems with already loaded ascii data.

Post a Comment for "Python Change Character Encoding To Utf_8"