首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python3.5 BytesIO错误

Python3.5 BytesIO错误
EN

Stack Overflow用户
提问于 2016-07-03 20:10:42
回答 2查看 3.6K关注 0票数 1

我正在用python2.7编写一个项目,但由于文档是python3.5,所以它开始在最后部分给我一些错误。因此,我将所有内容更改为python3.5,但由于bytesIO,它给了我一个错误。你能帮我理解为什么,我该怎么做?错误来自于def repr on string_dinamica.write('P3\n')。我把所有的代码都留下了,以防需要它。谢谢你的帮助。注意:只是为了确认这在python2.7上是可行的,但在3.5中就不行了。

代码语言:javascript
运行
复制
from io import BytesIO
from cor_rgb_42347 import CorRGB

class Imagem:
    def __init__(self, numero_linhas, numero_colunas):
        self.numero_linhas = numero_linhas
        self.numero_colunas = numero_colunas
        self.linhas = []
        for n in range(numero_linhas):
            linha = []
            for m in range(numero_colunas):
                linha.append(CorRGB(0.0, 0.0, 0.0))
            self.linhas.append(linha)

    def __repr__(self):
        string_dinamica = BytesIO()

        string_dinamica.write('P3\n')
        string_dinamica.write("#mcg@leim@isel 2015/16\n")
        string_dinamica.write(str(self.numero_colunas) + " " \
                              + str(self.numero_linhas) + "\n")
        string_dinamica.write("255\n")
        for linha in range(self.numero_linhas):
            for coluna in range(self.numero_colunas):
                string_dinamica.write(str(self.linhas[linha][coluna])+ " ")
            string_dinamica.write("\n")

        resultado = string_dinamica.getvalue()

        string_dinamica.close()

        return resultado


    def set_cor(self, linha, coluna, cor_rgb):
        """Permite especificar a cor RGB do pixel na linha "linha",
        coluna "coluna".
        """
        self.linhas[linha-1][coluna-1] = cor_rgb

    def get_cor(self, linha, coluna):
        """Permite obter a cor RGB do pixel na linha "linha",
        coluna "coluna".
        """
        return self.linhas[linha-1][coluna-1]

    def guardar_como_ppm(self, nome_ficheiro):

        """Permite guardar a imagem em formato PPM ASCII num ficheiro.
        """
        ficheiro = open(nome_ficheiro, 'w')
        ficheiro.write(str(self))
        ficheiro.close()



if __name__ == "__main__":
    imagem1 = Imagem(5,5)
    print(imagem1)




 Traceback (most recent call last):
  File "C:\Users\Utilizador\Desktop\Projectos Finais\Projecto_42347\imagem_42347.py", line 60, in <module>
    print(imagem1)
  File "C:\Users\Utilizador\Desktop\Projectos Finais\Projecto_42347\imagem_42347.py", line 19, in __repr__
    string_dinamica.write('P3\n')
TypeError: a bytes-like object is required, not 'str'
EN

回答 2

Stack Overflow用户

发布于 2016-07-04 04:40:01

对于Python3,只需将BytesIO更改为StringIO即可。Python3字符串是Unicode字符串,而不是字节字符串,__repr__应该在Python3中返回一个Unicode字符串。

如果您试图返回一个字节对象,就像其他一些答案所建议的那样,您将得到:

代码语言:javascript
运行
复制
TypeError: __repr__ returned non-string (type bytes)
票数 2
EN

Stack Overflow用户

发布于 2016-07-04 04:39:16

正如我在评论中提到的,BytesIO需要byte-like object

演示:

代码语言:javascript
运行
复制
>>> from io import BytesIO
>>> 
>>> b = BytesIO()
>>> 
>>> b.write('TEST\n')
Traceback (most recent call last):
  File "<pyshell#97>", line 1, in <module>
    b.write('TEST\n')
TypeError: 'str' does not support the buffer interface
>>> 
>>> 
>>> b.write(b'TEST\n')
5
>>> v = b.getbuffer()
>>> 
>>> v[2:4]=b'56'
>>> 
>>> b.getvalue()
b'TE56\n'

所以,在你的副词开头再加一句。在传递给write方法时,b(用于二进制)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38173918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档