Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTF-8 bytes to some other encoding in Python

I need to do in Python 2.4 (yes, 2.4 :-( ).

I've got a plain string object, which represents some text encoded with UTF-8. It comes from an external library, which can't be modified.

So, what I think I need to do, is to create an Unicode object using bytes from that source object, and then convert it to some other encoding (iso-8859-2, actually).

The plain string object is 'x'. "unicode()" seems to not work:

>>> x
'Sk\xc5\x82odowski'
>>> str(unicode(x, encoding='iso-8859-2'))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-3: ordinal not in range(128)
>>> unicode(x, encoding='iso-8859-2')
u'Sk\u0139\x82odowski'
like image 380
lollo Avatar asked Jul 03 '26 06:07

lollo


1 Answers

>>> x.decode('utf8').encode('iso-8859-2')
'Sk\xb3odowski'
like image 92
YOU Avatar answered Jul 05 '26 20:07

YOU



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!