当前位置: 动力学知识库 > 问答 > 编程问答 >

python - python3 convert byte class to string

问题描述:

I receive data from a socket in Python 3 with

data = ""

...

data = s.recv(1024)

When I want to print(data) and print (type(data)), it tells me:

b'contentofdata'

<class 'bytes'>

How can I convert data back to a string so it only shows contentofdata after print(data)? I tried a lot with .decode or .encode, but it does not seem to work in Python 3.

网友答案:

You can just do str(data, 'utf-8') to convert bytes to string.

分享给朋友:
您可能感兴趣的文章:
随机阅读: