When I trying to encode cyrillic “Р” character I get errors. Here is my code and errors:
>>> "Р".encode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
How to solve it. Please help me. I’m using Python 2.7.x. Thank you for every advice.
EDITED:
def _to_unicode_or_bust(self, obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
I get above method from presentation. It’s work in terminal and simple python file. It’s not work in OpenERP.
Anything in
""(e.g. astr) in Python 2.x is already encoded. You need to decode it to aunicodebefore you can encode it as something else.“Unicode In Python, Completely Demystified”