python – Convert bytes to a string

python – Convert bytes to a string

How-to-Sort-Dictionary-By-Value-in-Python-3.7

You need to decode the bytes object to produce a string:

>>> babcde
babcde

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> babcde.decode(utf-8) 
abcde

See: https://docs.python.org/3/library/stdtypes.html#bytes.decode

You need to decode the byte string and turn it in to a character (Unicode) string.

On Python 2

encoding = utf-8
hello.decode(encoding)

or

unicode(hello, encoding)

On Python 3

encoding = utf-8
bhello.decode(encoding)

or

str(bhello, encoding)

python – Convert bytes to a string

I think this way is easy:

>>> bytes_data = [112, 52, 52]
>>> .join(map(chr, bytes_data))
p44

Leave a Reply

Your email address will not be published. Required fields are marked *