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。


当前回答

只使用

base64.b64decode(a) 

而不是

base64.b64decode(a).decode('utf-8')

其他回答

如果可能,在文本编辑器中打开该文件,并尝试将编码更改为UTF-8。否则,在操作系统级别以编程方式进行。

这仅仅意味着选择了错误的编码来读取文件。

在Mac上,使用file -I file.txt查找正确的编码。在Linux操作系统中,请使用file -i file.txt。

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

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

您必须使用latin1编码来读取该文件,因为该文件中有一些特殊字符,使用下面的代码片段来读取该文件。

这里的问题是编码类型。当Python不能将数据转换为读取时,它会给出一个错误。

您可以使用latin1或其他编码值。

我建议您尝试并测试为您的数据集找到正确的方法。

有一个类似的问题,最后使用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格式返回代码。从那里开始,它将被解码并以行分隔。