foote

Convert bytes to a string


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

>>> b"abcde"
b'abcde'

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

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'
b'hello'.decode(encoding)


or



str(b'hello', encoding)


Convert bytes to a string Convert bytes to a string Reviewed by Udochi V.C on January 02, 2022 Rating: 5

No comments:

Powered by Blogger.