How to convert string to bytes in Python 2

laike9m

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?

Martijn Pieters

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')
岁

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert string to bytes in Python 2

From Dev

How to convert list of bytes (unicode) to Python string?

From Dev

How do I convert a string into a string of bytes in python 2.7

From Dev

How to convert binary string to the byte array of 2 bytes in java

From Dev

How to convert bytearray with non-ASCII bytes to string in python?

From Dev

convert an integer into 2 bytes in python

From Java

Best way to convert string to bytes in Python 3?

From Dev

How to convert array of UCS-2 bytes to UTF-8 string in Ruby?

From Dev

In Python, how to convert array of bits to array of bytes?

From Dev

How does Python convert bytes into float?

From Java

Convert bytes to a string

From Dev

Convert string to bytes literally

From Dev

Convert stream bytes to string

From Dev

String to bytes in both Python 2 and 3

From Dev

How to convert a python string

From Dev

Convert from string containing hexadecimal characters to bytes in python 3

From Dev

Efficient way to split a bytes array then convert it to string in Python

From Dev

Python error: Can't convert bytes to string implicitly

From Dev

Efficient way to split a bytes array then convert it to string in Python

From Dev

What is the correct way to convert bytes to a hex string in Python 3?

From Dev

How to split a byte string into separate bytes in python

From Dev

How do I convert a string into a vector of bytes in rust?

From Java

How do I convert a Vector of bytes (u8) to a string

From Dev

How to convert an array of bytes into string with Node.js?

From Dev

How to convert specific bytes from binary file into string most efficiently

From Dev

In Perl, how can I convert an array of bytes to a Unicode string?

From Dev

How to convert/paste a hex string as a list of bytes in vim?

From Dev

how can I convert my String (that represents hex values) to bytes?

From Dev

How to convert hex string to hex bytes format in PySpark

Related Related

  1. 1

    How to convert string to bytes in Python 2

  2. 2

    How to convert list of bytes (unicode) to Python string?

  3. 3

    How do I convert a string into a string of bytes in python 2.7

  4. 4

    How to convert binary string to the byte array of 2 bytes in java

  5. 5

    How to convert bytearray with non-ASCII bytes to string in python?

  6. 6

    convert an integer into 2 bytes in python

  7. 7

    Best way to convert string to bytes in Python 3?

  8. 8

    How to convert array of UCS-2 bytes to UTF-8 string in Ruby?

  9. 9

    In Python, how to convert array of bits to array of bytes?

  10. 10

    How does Python convert bytes into float?

  11. 11

    Convert bytes to a string

  12. 12

    Convert string to bytes literally

  13. 13

    Convert stream bytes to string

  14. 14

    String to bytes in both Python 2 and 3

  15. 15

    How to convert a python string

  16. 16

    Convert from string containing hexadecimal characters to bytes in python 3

  17. 17

    Efficient way to split a bytes array then convert it to string in Python

  18. 18

    Python error: Can't convert bytes to string implicitly

  19. 19

    Efficient way to split a bytes array then convert it to string in Python

  20. 20

    What is the correct way to convert bytes to a hex string in Python 3?

  21. 21

    How to split a byte string into separate bytes in python

  22. 22

    How do I convert a string into a vector of bytes in rust?

  23. 23

    How do I convert a Vector of bytes (u8) to a string

  24. 24

    How to convert an array of bytes into string with Node.js?

  25. 25

    How to convert specific bytes from binary file into string most efficiently

  26. 26

    In Perl, how can I convert an array of bytes to a Unicode string?

  27. 27

    How to convert/paste a hex string as a list of bytes in vim?

  28. 28

    how can I convert my String (that represents hex values) to bytes?

  29. 29

    How to convert hex string to hex bytes format in PySpark

HotTag

Archive