解决UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
来源:互联网 发布:阿里云 国际版 编辑:程序博客网 时间:2024/04/29 20:14
关于利用pickle.load()载入文件出现如下错误的解决办法。
UnicodeDecodeError Traceback (most recent call last)<ipython-input-16-9506c06e646a> in <module>() 1 # Load the raw CIFAR-10 data. 2 cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'----> 3 X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir) 4 5 # As a sanity check, we print out the size of the training and test data.D:\python3wp\assignment1\cs231n\data_utils.py in load_CIFAR10(ROOT) 20 for b in range(1,6): 21 f = os.path.join(ROOT, 'data_batch_%d' % (b, ))---> 22 X, Y = load_CIFAR_batch(f) 23 xs.append(X) 24 ys.append(Y)D:\python3wp\assignment1\cs231n\data_utils.py in load_CIFAR_batch(filename) 7 """ load single batch of cifar """ 8 with open(filename, 'rb') as f:----> 9 datadict = pickle.load(f) 10 X = datadict['data'] 11 Y = datadict['labels']UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
首先给出pickle.load()的函数解释:
pickle.load()
pickle.load(file, *, fix_imports=True, encoding=”ASCII”, errors=”strict”)
Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein. This is equivalent to Unpickler(file).load().
The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.
The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return bytes. Thus file can be an on-disk file opened for binary reading, an io.BytesIO object, or any other custom object that meets this interface.
Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is true, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.
import pickleli = [1, 2, 3]pickle.dump(li, open('db', 'wb'))ret = pickle.load(open('db', 'rb'))print(ret)
也就是说pickle.load()默认解码是以encoding=”ASCII”解码的,而我们要载入的文件并不是以”ASCII”形式存储的,所以要改变参数encoding=” ”
解决方式如下:
pickle.load(f,encoding='bytes')
这样编码可以用“字节”来读取这些8位字符串实例作为字节对象。
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
- 解决 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128) 解决
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128) 解决
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
- 解决unicodedecodeerror ascii codec can’t decode byte 0xd7 in position 9 ordinal not in range(128)
- 解决unicodedecodeerror ascii codec can’t decode byte 0xd7 in position 9 ordinal not in range(128)
- 解决unicodedecodeerror ascii codec can’t decode byte 0xd7 in position 9 ordinal not in range(128)
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 108: ordinal not in range(128
- 解决unicodedecodeerror ascii codec can’t decode byte 0xd7 in position 9 ordinal not in range(128)
- 解决unicodedecodeerror ascii codec can’t decode byte 0xd7 in position 9 ordinal not in range(128)
- 解决“UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 3: ordinal not in range(128”
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
- 解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xcf in position 7: ordinal not in range(128)
- "设置--安全--未知来源“默认打开
- solr在windows下的安装使用(一)
- 《剑指offer》刷题笔记(时间空间效率的平衡):丑数
- 唯快不破:TCP/IP详解--ICMP 控制报文和差错报文 && Tracerouter 命令的过程
- crontab
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
- 自顶向下,逐步求精
- logstash使用之json输入输出
- CVPR 2013 papers
- python面向对象
- 对刚入职场的大学生的建议
- banner适配器封装
- 吴恩达 机器学习笔记四(lecture 4)(多特征值,梯度下降与Normal equation)
- JAVA | 20