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.

推荐答案

Python试图将字节数组(bytes,它假定为utf-8编码的字符串)转换为unicode字符串(str).这个过程当然是根据utf-8规则进行解码.try 此操作时,它会遇到utf-8编码字符串中不允许的字节序列(即位置0处的0xff).

由于您没有提供任何代码,我们只能猜测其他代码.

根据堆栈跟踪,我们可以假设触发动作是从文件(contents = open(path).read())读取.我建议以这样的方式重新编码:

with open(path, 'rb') as f:
  contents = f.read()

open()中模式说明符中的b表示该文件应被视为二进制文件,因此contents将保持bytes.这样就不会进行解码try .

Python相关问答推荐

如何从维基百科的摘要部分/链接列表中抓取链接?

如何在Pandas 中存储二进制数?

pyramid 内部数组中的连续序列-两极

如何使用PyTest根据self 模拟具有副作用的属性

这家einsum运营在做什么?E = NP.einsum(aj,kl-il,A,B)

如何将我的位置与光强度数据匹配到折射图案曲线中?

Twilio:CallInstance对象没有来自_的属性'

在Python和matlab中显示不同 colored颜色 的图像

如何根据日期和时间将状态更新为已过期或活动?

如何使用Google Gemini API为单个提示生成多个响应?

Pydantic 2.7.0模型接受字符串日期时间或无

实现自定义QWidgets作为QTimeEdit的弹出窗口

计算每个IP的平均值

根据列值添加时区

如何在turtle中不使用write()来绘制填充字母(例如OEG)

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

Python列表不会在条件while循环中正确随机化'

Python Pandas获取层次路径直到顶层管理

如何使regex代码只适用于空的目标单元格

未调用自定义JSON编码器