python3 读文件编码错误

Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/yangguang/machineLearning/learn_machineLearning/Tensorflow_learning/cnn_own/data_prepare/src/tfrecord.py", line 129, in _process_image_files_batch
    image_buffer, height, width = _process_image(filename, coder)
  File "/home/yangguang/machineLearning/learn_machineLearning/Tensorflow_learning/cnn_own/data_prepare/src/tfrecord.py", line 71, in _process_image
    image_data = f.read()
  File "/usr/lib/python3.6/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

INFO:root:2018-05-25 11:11:55.104869: Finished writing all 4800 images in data set.

运行代码时发现以上错误,原因是read的文件是binary格式,解决办法:

with open(filename, 'r') as f:
    image_data = f.read()

将read方法从  'r'  改成   'rb ',b表示binary,即可


tensorflow里的BYTELIST 在python2里可以直接传入字符串,在python3里则需要先转成bytes类型:

text_b = bytes(text, encoding='utf-8')

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值