Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.5字节错误_Python_Python 2.7_Python 3.x_Bytesio - Fatal编程技术网

Python 3.5字节错误

Python 3.5字节错误,python,python-2.7,python-3.x,bytesio,Python,Python 2.7,Python 3.x,Bytesio,我正在用Python2.7制作一个项目,但由于文档是用Python3.5编写的,所以它在最后部分给了我一些错误。因此,我将所有内容都更改为Python3.5,但由于bytesIO,它给了我一个错误。你能帮我理解为什么,我该怎么做吗?错误来自字符串_dinamica.write('P3\n')上的defrepr。我留下了所有的代码以备不时之需。谢谢你的帮助。注意:只是为了确认这在Python2.7上有效,但在3.5中不起作用 from io import BytesIO from cor_rgb_

我正在用Python2.7制作一个项目,但由于文档是用Python3.5编写的,所以它在最后部分给了我一些错误。因此,我将所有内容都更改为Python3.5,但由于bytesIO,它给了我一个错误。你能帮我理解为什么,我该怎么做吗?错误来自字符串_dinamica.write('P3\n')上的defrepr。我留下了所有的代码以备不时之需。谢谢你的帮助。注意:只是为了确认这在Python2.7上有效,但在3.5中不起作用

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'
从io导入字节io
从cor_rgb_42347导入CorRGB
类别Imagem:
定义初始值(self、numero\u linhas、numero\u colunas):
self.numero\u linhas=numero\u linhas
self.numero\u colunas=numero\u colunas
self.linhas=[]
对于范围内的n(数量):
林哈=[]
对于范围内的m(数值):
追加(CorRGB(0.0,0.0,0.0))
self.linhas.append(linha)
定义报告(自我):
字符串_dinamica=BytesIO()
string_dinamica.write('P3\n')
字符串_dinamica.write(“#mcg@leim@isel 2015/16\n“
string_dinamica.write(str(self.numero_colunas)+“\
+str(自编号)+“\n”)
字符串_dinamica.write(“255\n”)
对于范围内的linha(自编号):
对于范围内的coluna(自编号):
string_dinamica.write(str(self.linhas[linha][coluna])+“”)
字符串\u dinamica.write(“\n”)
resultado=string_dinamica.getvalue()
字符串_dinamica.close()
返回结果
def设置校正(self、linha、coluna、cor_rgb):
“”“Permite Specificiar a cor RGB do pixel na linha”“linha”,
科鲁纳“科鲁纳”。
"""
self.linhas[linha-1][coluna-1]=cor_rgb
def get_cor(赛尔夫、林哈、科鲁纳):
“”“Permite obter a cor RGB do pixel na linha”“linha”,
科鲁纳“科鲁纳”。
"""
返回自我。林哈斯[linha-1][coluna-1]
def guardar_como_ppm(自身、名称):
“”“Permite guardar a imagem em formato PPM ASCII num ficheiro。
"""
菲希罗=开放(命名为“w”)
ficheiro.write(str(self))
菲切罗·克洛斯()
如果名称=“\uuuuu main\uuuuuuuu”:
imagem1=Imagem(5,5)
打印(图像M1)
回溯(最近一次呼叫最后一次):
文件“C:\Users\Utilizador\Desktop\Projectos Finais\Projecto_42347\imagem_42347.py”,第60行,在
打印(图像M1)
文件“C:\Users\Utilizador\Desktop\Projectos Finais\Projecto_42347\imagem_42347.py”,第19行,在报告中__
string_dinamica.write('P3\n')
TypeError:需要类似字节的对象,而不是“str”

正如我在评论中提到的,需要

演示:

>>从io导入字节io
>>> 
>>>b=字节()
>>> 
>>>b.write('TEST\n')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
b、 写入('TEST\n')
TypeError:“str”不支持缓冲区接口
>>> 
>>> 
>>>b.write(b'TEST\n')
5.
>>>v=b.getbuffer()
>>> 
>>>v[2:4]=b'56'
>>> 
>>>b.getvalue()
b'TE56\n'

因此,在参数的开头添加参数。在传递给
write
方法时,b(表示二进制)。

对于Python 3,只需将
BytesIO
更改为
StringIO
。Python3字符串是Unicode字符串而不是字节字符串,
\uuuu repr\uuu
应该在Python3中返回Unicode字符串

如果您尝试像其他答案所建议的那样返回bytes对象,您将得到:

TypeError: __repr__ returned non-string (type bytes)

你能发布回溯信息吗?也许,你只需要做一个参数。以…的形式传递给
write
方法,因此:
string\u dinamica.write(b'P3\n')
即使我们以
b'Test'
的形式传递参数,Python 3上的@IronFist
必须返回Unicode字符串。将
b'Test'
传递到
BytesIO
只会修复该行上的错误,但是当返回
resultado
时,它将得到所描述的
TypeError
I。
TypeError: __repr__ returned non-string (type bytes)