[英]How do I change the integer axis labels on a seaborn chart to my own strings?
[英]How do I change my data from bytes to strings in this function?
我有一些用于联网程序的代码。 及其
def dataReceived(self, data)
print(f"Received quote: {data}")
self.transport.loseConnection()
此功能正在打印
Received quote: b'\x00&C:\\Users\\.pycharm2016.3\\config\x00&C:\\users\\pycharm\\system\x00\x03--'
我将如何更改代码以解决此问题?
我想我知道正在打印的是字节代码,需要将其转换为字符串,但是我要在程序的服务器或客户端执行此操作吗?
当我写
print(f"receivedquote: {data}".decode('utf-8')
不能解决问题。 我收到很多错误。 我如何更好地问这个问题以找到解决方案?
解码实际数据:
data = data.decode('utf-8')
此时, data
是Python Unicode字符串,您可以打印,搜索,切片等。
(您的数据看上去并不特别像UTF-8,但我不会对此进行猜测;显然也不是不是UTF-8。)
通常,在接收到字节字符串后立即转换为Unicode,并让程序的其余部分对字符串进行操作是一个好主意。 如果您需要编码回字节,则类似地,仅在数据离开程序之前伸出周边。 ( Ned Batchelder将此称为“ Unicode三明治”。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.