python - 在python中将字节转换为字符串

标签 python sockets encoding decoding

我有以下使用套接字从另一个系统返回的打印字节:
b"\x0bMessage Received!\r\x1c\r"
例如:print(b"\x0bMessage Received!\r\x1c\r".decode(encoding="utf-8"))
我得到了
您能帮我了解如何从该消息中获取类似Message Received!的输出。

最佳答案

您需要去除不需要的字符(在这种情况下为垂直制表符和回车符):

>>> bs = b"\x0bMessage Received!\r\x1c\r"
>>> print(bs.decode().strip())
Message Received!

关于python - 在python中将字节转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60544929/

相关文章:

python - 是否有内置的 dict.get() 的递归版本?

java - Java应用程序中CPU核心数和线程数之间的关系是什么?

c# - 将拆分视频保存到数据库中并检索它们

Python beam 搜索生成相同序列的 Keras LSTM 模型

python - scikit 学习 : update countvectorizer after selecting k best features

python - 波形文件如何存储多个 channel ?

java - 从服务器端为特定网站打开的所有浏览器之间的通信

python - ZeroMQ 和 Python 中的多个订阅过滤器

ruby-on-rails - ActiveRecord 在 Heroku 上错误地加载二进制字段,在 OSX 上正常

angular - CentOs - Tomcat - Angular2 - 编码 UTF-8