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, encodi开发者_如何学Pythonng='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'
>>> x.decode('utf8').encode('iso-8859-2')
'Sk\xb3odowski'
精彩评论