https://github.com/affinelayer/pix2pix-tensorflow/tree/master/tools

在上面的站点上编译“process.py”时发生错误。

 python tools/process.py --input_dir data --            operation resize --outp
ut_dir data2/resize
data/0.jpg -> data2/resize/0.png

回溯(最近一次调用):

File "tools/process.py", line 235, in <module>
  main()
File "tools/process.py", line 167, in main
  src = load(src_path)
File "tools/process.py", line 113, in load
  contents = open(path).read()
      File"/home/user/anaconda3/envs/tensorflow_2/lib/python3.5/codecs.py", line 321, in decode
  (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode     byte 0xff in position 0: invalid start byte

错误的原因是什么? Python的版本是3.5.2。


当前回答

我对PNG文件也有类似的问题。我尝试了以上的解决方案,但都没有成功。 这个在python 3.8中对我有用

with open(path, "rb") as f:

其他回答

检查要读取的文件的路径。我的代码不断给我错误,直到我改变路径名,以呈现工作目录。错误是:

newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

我在处理从Linux生成的文件时遇到了同样的问题。事实证明,这与包含问号的文件有关。

以下代码在我的情况下工作:

Df = pd。Read_csv (filename,sep =' \t', encoding='cp1252')

有一个类似的问题,最后使用UTF-16解码。我的代码如下。

with open(path_to_file,'rb') as f:
    contents = f.read()
contents = contents.rstrip("\n").decode("utf-16")
contents = contents.split("\r\n")

这将把文件内容作为导入,但它将以UTF格式返回代码。从那里开始,它将被解码并以行分隔。

我也遇到过类似的问题。

解决方法:

import io

with io.open(filename, 'r', encoding='utf-8') as fn:
  lines = fn.readlines()

然而,我遇到了另一个问题。一些html文件(在我的情况下)不是utf-8,所以我收到了类似的错误。当我排除这些html文件时,一切都很顺利。

所以,除了修复代码,还要检查你正在读取的文件,也许确实存在不兼容性。