Python TypeError:使用pickle需要一个整数(获取类型_io.BufferedWriter)
守则:Python TypeError:使用pickle需要一个整数(获取类型_io.BufferedWriter),python,file,serialization,pickle,Python,File,Serialization,Pickle,守则: import pickle test = 3 >>> with open('test', 'wb') as file: ... pickle.dumps(test, file) 和意外报告的错误 Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: an integer is required (got type _io.
import pickle
test = 3
>>> with open('test', 'wb') as file:
... pickle.dumps(test, file)
和意外报告的错误
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: an integer is required (got type _io.BufferedWriter)
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
TypeError:需要一个整数(获取类型_io.BufferedWriter)
这是怎么回事?您使用了错误的函数。以下是文件: 返回对象作为
bytes
对象的pickle表示
转储
将传递的对象转换为字节
并返回它。当您将文件参数传递给.dump
预期的pickling协议时,会出现错误,该协议应为整数
您需要使用pickle.dump
,它实际上会转储到一个文件:
将obj
的pickle表示写入打开的文件对象文件
dumps(obj, protocol=None, *, fix_imports=True)
dump(obj, file, protocol=None, *, fix_imports=True)
with open('test', 'wb') as file:
pickle.dump(test, file)