【问题标题】:python3 convert byte class to stringpython3将字节类转换为字符串
【发布时间】:2016-01-15 16:59:16
【问题描述】:

我从 Python 3 中的套接字接收数据

data = ""
...
data = s.recv(1024)

当我想print(data)print (type(data)) 时,它告诉我:

b'contentofdata'
<class 'bytes'>

如何将data 转换回字符串,使其仅在print(data) 之后显示contentofdata?我对.decode.encode 进行了很多尝试,但它似乎不适用于Python 3。

【问题讨论】:

  • 只是data.decode()

标签: python python-3.x encode


【解决方案1】:

您可以通过 str(data, 'utf-8') 将字节转换为字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2021-06-23
    • 2015-10-24
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多