Example:
>>> s = "Plébiscité sur la toile">>> s'Pl\xc3\xa9biscit\xc3\xa9 sur la toile'>>> print sPlébiscité sur la toile>>> type(s)<type 'str'>
Change s variable encoding :
>>> s = u"Plébiscité sur la toile">>> type(s)<type 'unicode'>>>> s = u"Plébiscité sur la toile".encode('utf-8')>>> s'Pl\xc3\xa9biscit\xc3\xa9 sur la toile'>>> type(s)<type 'str'>
Another example:
>>> s = "Plébiscité sur la toile">>> type(s)<type 'str'>>>> s = unicode(s,'utf-8')>>> su'Pl\xe9biscit\xe9 sur la toile'>>> type(s)<type 'unicode'>
Note In a python script to fix the error message:
#SyntaxError: Non-ASCII character '\x96' in file MyFile.py on line XX, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
just add at the first line of the python scriptL
# -*- coding: utf-8 -*-
References
| Links | Site |
|---|---|
| Unicode HOWTO | Python Documentation |
| byte string vs. unicode string. Python | stackoverflow |
| How can I display native accents to languages in console in windows? | stackoverflow |
| Python detect string byte encoding | stackoverflow |
| How do I check if a string is unicode or ascii? | stackoverflow |
| How to detect the encoding of a file? | stack exchange |
| Unicode literals that work in python 3 and 2 | stackoverflow |
