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。


当前回答

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

其他回答

如果你在mac上,检查是否有一个隐藏文件,. ds_store。删除文件后,我的程序工作。

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

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

我在尝试使用pandas.read_csv()读取“.csv”文件时遇到了这个UnicodeDecodeError。在我的例子中,我无法使用其他编码器类型来克服这个问题。但是我们不用

pd.read_csv(filename, delimiter=';')

我使用:

pd.read_csv(open(filename, 'r'), delimiter=';')

这对我来说还行。

注意:在open()函数中,使用'r'而不是'rb'。因为'rb'返回的bytes对象首先导致了这个解码器错误,这与read_csv()中的问题相同。但是'r'返回str,这是需要的,因为我们的数据是.csv格式的,并且使用默认的encoding='utf-8'参数,我们可以使用read_csv()函数轻松地解析数据。

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

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

使用ISO-8859-1编码格式解决此问题。