python gbk转utf8,在Python中将GBK转换为utf8字符串

I have a string.

s = u""

How can I translate s into a utf-8 string? I have tried s.decode('gbk').encode('utf-8') but python reports error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 35-50: ordinal not in range(128)

解决方案

in python2, try this to convert your unicode string:

>>> s.encode('latin-1').decode('gbk')

u""

then you can encode to utf-8 as you wish.

>>> s.encode('latin-1').decode('gbk').encode('utf-8')

""

你可能感兴趣的:(python,gbk转utf8)