[英]TypeError: an integer is required (got type _io.BufferedWriter) using pickle
the code:代码:
import pickle
test = 3
>>> with open('test', 'wb') as file:
... pickle.dumps(test, file)
and error reported unexpectedly.和错误报告意外。
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: an integer is required (got type _io.BufferedWriter)
What's going on here?这是怎么回事?
You're using the wrong function.您使用了错误的功能。 Here's the docs:这是文档:
dumps(obj, protocol=None, *, fix_imports=True)
Return the pickled representation of the object as a
bytes
object.返回对象的腌渍表示作为bytes
对象。
dumps
converts the passed object to bytes
and returns it. dumps
将传递的对象转换为bytes
并返回它。 The error you get is when you pass a file argument to what .dump
expects to be the pickling protocol, which is supposed to be an integer.你得到的错误是当你将文件参数传递给.dump
期望的酸洗协议时,它应该是一个整数。
You'll want to use pickle.dump
, which actually dumps to a file:你会想要使用pickle.dump
,它实际上转储到一个文件:
dump(obj, file, protocol=None, *, fix_imports=True)
Write a pickled representation of
obj
to the open file objectfile
.将obj
的pickle 表示写入打开的文件目标file
。
with open('test', 'wb') as file:
pickle.dump(test, file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.