【问题标题】:TypeError: 'str' does not support the buffer interface using Python3.xTypeError: 'str' 不支持使用 Python3.x 的缓冲区接口
【发布时间】:2015-06-23 21:02:37
【问题描述】:

我刚刚从 python 2.x 迁移到 python 3.x,下面的代码已经停止工作。

    #self.request is the TCP socket connected to the client
    self.data = self.request.recv(1024).strip()
    print "{} wrote:".format(self.client_address[0])
    print self.data

    words = self.data.split(',') //the problem seems to be here

知道如何解决这个问题吗?谢谢!

【问题讨论】:

    标签: sockets python-3.x


    【解决方案1】:

    发生这种情况是因为 python 3 中的 bytesstr 不同。 request.recv 给你的是一个bytes 的数据。您需要先将其转换为str,然后再将其转换为split

    您可以进行utf-8 解码。所以像 -

    self.data.decode('utf-8').split(',') should work. 
    

    How to convert between bytes and strings in Python 3?有更详细的解释。

    【讨论】:

      猜你喜欢
      • 2016-05-08
      • 2011-07-25
      • 2014-08-09
      • 2023-03-10
      • 2015-01-12
      • 1970-01-01
      相关资源
      最近更新 更多