开发者

Convert UTF-8 bytes to some other encoding in Python

开发者 https://www.devze.com 2022-12-24 04:51 出处:网络
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.

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消