Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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修改非文本文件_Python_Image_Python 3.x_Utf 8_Utf8 Decode - Fatal编程技术网

通过Python 3修改非文本文件

通过Python 3修改非文本文件,python,image,python-3.x,utf-8,utf8-decode,Python,Image,Python 3.x,Utf 8,Utf8 Decode,我正在做一个加密/解密程序,我让它在文本文件上工作;但是,我无法打开任何其他格式。例如,如果我这样做: a_file = open('C:\Images\image.png', 'r', encoding='utf-8') for a_line in a_file: print(a_line) 我得到: Traceback (most recent call last): File "<stdin>", line 1, in <module> File

我正在做一个加密/解密程序,我让它在文本文件上工作;但是,我无法打开任何其他格式。例如,如果我这样做:

a_file = open('C:\Images\image.png', 'r', encoding='utf-8')
for a_line in a_file:
    print(a_line)
我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Comp_Sci/Coding/line_read_test.py", line 2, in <module>
for a_line in a_file:
File "C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第685行,在runfile中
execfile(文件名、命名空间)
文件“C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\site packages\spyderlib\widgets\externalshell\sitecustomize.py”,第85行,在execfile中
exec(编译(打开(文件名'rb').read(),文件名'exec'),命名空间)
文件“C:/Comp_Sci/Coding/line_read_test.py”,第2行,在
对于\u文件中的\u行:
文件“C:\WinPython-64bit-3.4.3.4\python-3.4.3.amd64\lib\codecs.py”,第319行,解码
(结果,消耗)=自身缓冲区解码(数据,自身错误,最终)
UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0x89:无效的开始字节

我做错了什么?

您将其作为文本文件打开,并假设您可以阅读其中的行并有意义地打印其中的任何内容

with open(r'C:\Images\image.png', 'rb') as a_file:
  while True:
    data = a_file.read(32)
    if not data:
      break
    print(data)

简短版本:您正在以文本模式打开二进制文件。使用
'rb'
而不是
'r'
(并删除
编码
参数),您将正确地执行此操作

长版本:Python3对ByTestRing和Unicode字符串进行了非常严格的区分。
str
类型只包含Unicode字符串;
str
的每个字符都是一个Unicode码点。另一方面,
字节
类型表示一系列不一定与文本对应的8位值。例如,.PNG文件应作为
bytes
对象加载,而不是作为
str
对象加载。通过将
encoding=“utf-8”
参数传递给
open()
,您告诉Python您的文件只包含有效的utf-8文本,而.PNG显然不包含。相反,您应该使用
'rb'
将文件作为二进制文件打开,而不使用任何编码。然后,当您读取文件时,您将获得
字节
对象,而不是
str
对象,您需要以不同的方式对待它们

我看到@ignacio vazquez abrams在我输入这个答案时已经发布了很好的示例代码,所以我不会重复他的工作。他的代码是正确的:使用它,你会没事的