什么是"file_like_object",什么是"file"; pickle.load()和pickle.loads() [英] what is "file_like_object", what is "file"; pickle.load() and pickle.loads()

查看:229
本文介绍了什么是"file_like_object",什么是"file"; pickle.load()和pickle.loads()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在弄清pickle.load()pickle.loads()之间的区别.有人说pickle.load()处理的对象是"file_like_object",但是pickle.loads()对应于文件对象".

I am figuring out the differences between the pickle.load() and pickle.loads(). Somebody said what kind of object that pickle.load() process is "file_like_object", however, pickle.loads() corresponds to "file object".

推荐答案

要使用的函数的选择取决于加载腌制数据的对象:

Your choice of which function to use depends on the object from whence you are loading the pickled data:

pickle.loads 用于从中加载腌制数据bytes字符串. loads中的"s"是指在Python 2中,数据是从 string 加载的.

pickle.loads is used to load pickled data from a bytes string. The "s" in loads refers to the fact that in Python 2, the data was loaded from a string.

例如:

import pickle

with open("myobj.pickle", "rb") as f:
    rawdata = f.read()

myobj = pickle.loads(rawdata)


pickle.load 用于从中加载腌制数据类似文件的对象.这是任何行为类似于文件的对象-在这种情况下,这意味着它具有返回bytesread()方法.


pickle.load is used to load pickled data from a file-like object. This is any object that acts like a file - in this case, meaning it has a read() method that returns bytes.

例如:

import pickle

with open("myobj.pickle", "rb") as f:
    myobj = pickle.load(f)


相同的约定适用于pickle库中的dump/dumps函数以及json模块和其他模块.


This same convention applies to the dump/dumps functions in the pickle library, as well as the json module and others.

这篇关于什么是"file_like_object",什么是"file"; pickle.load()和pickle.loads()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆