Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to bytes in Python 2

I know this may sounds like a duplicate question, but that's because I don't know how to describe this question properly.

For some reason I got a bunch of unicode string like this:

a = u'\xcb\xea'

As you can see, it's actually bytes representation of a Chinese character, encoding in gbk

>>> print(b'\xcb\xea'.decode('gbk'))
岁

u'岁' is what I need, but I don't know how to convert u'\xcb\xea' to b'\xcb\xea'.
Any suggestions?

like image 482
laike9m Avatar asked Sep 11 '26 00:09

laike9m


1 Answers

It's not really a bytes representation, it's still unicode codepoints. They are the wrong codepoints, because it was decoded from bytes as if it was encoded to Latin-1.

Encode to Latin 1 (whose codepoints map one-on-one to bytes), then decode as GBK:

a.encode('latin1').decode('gbk')

Demo:

>>> a = u'\xcb\xea'
>>> a.encode('latin1').decode('gbk')
u'\u5c81'
>>> print a.encode('latin1').decode('gbk')
岁
like image 55
Martijn Pieters Avatar answered Sep 13 '26 13:09

Martijn Pieters



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!