你怎么用泡菜? [英] how do you use pickle?
问题描述
这就是我所拥有的:
导入泡菜
data = open(r''C:\ pickle_data。 txt'',''w'')
image = open(r''C:\ peakhell.jpg'')
pickle.Pickler(data)
data.dump(图片)
data.close()
image.close()
首先,我不确定第二行是否可以做到这一点。但是我的主要问题是我不知道如何在腌制
对象时定义步骤。你是如何首先创建对象的?它会返回一个值吗?我试过了:
pickler = pickle.Pickler(数据)
pickle.Pickler(数据)
然后我试过了:
pickler.dump(图片)
data.dump(图片)
基本上,我我只是对所涉及的所有语法感到困惑。哪个
对象我操作,什么时候?我无法在
文档或交互式帮助中找到这些答案,因为我没有看到任何示例。
此外,我每次都会收到此错误:
Traceback(最近一次调用最后一次):
文件" C:\Documents and Settings \jsaler01.TUFTS \\ \\ MY
Documents\Python24 \ _myscripts\challenges \ pickle.py" ,第3行,在-toplevel-
进口泡菜
文件C:\Documents and Settings\jsaler01.TUFTS \ Myy />
Documents\Python24\myscripts\challenges\pickle.py" ,第7行,在-toplevel-
pickle.Pickler(数据)
AttributeError:''module''对象没有属性''Pickler''
所以这让我很困惑,因为如果它甚至无法通过最初创建的pickler对象,我必须完全错误地使用语法
。
Here''s what I have:
import pickle
data = open(r''C:\pickle_data.txt'', ''w'')
image = open(r''C:\peakhell.jpg'')
pickle.Pickler(data)
data.dump(image)
data.close()
image.close()
First off, I''m not sure the second line is the way to do that. But my
main problem is I don''t know how to define the steps when pickling an
object. How do you first create the object? Does it return a value? I tried:
pickler = pickle.Pickler(data)
pickle.Pickler(data)
Then I tried:
pickler.dump(image)
data.dump(image)
Basically, I''m just confused about all the syntax involved. Which
objects do I operate on, and when? I can''t find these answers in the
documentation or in the interactive help, because I don''t see any examples.
Furthermore, I get this error every time:
Traceback (most recent call last):
File "C:\Documents and Settings\jsaler01.TUFTS\My
Documents\Python24\myscripts\challenges\pickle.py" , line 3, in -toplevel-
import pickle
File "C:\Documents and Settings\jsaler01.TUFTS\My
Documents\Python24\myscripts\challenges\pickle.py" , line 7, in -toplevel-
pickle.Pickler(data)
AttributeError: ''module'' object has no attribute ''Pickler''
So that makes me really confused, because I must have the syntax totally
wrong if it can''t even get past the initial creation of a pickler object.
推荐答案
John Salerno写道:
John Salerno wrote:
这就是我所拥有的:
>进口泡菜
数据=打开(r''C:\ pickle_data.txt'',''w'')
image = open(r''C:\ peakhell .jpg'')
pickle.Pickler(数据)
data.dump(图片)
data.close()
image.close()
>首先,我不确定第二行是否可以做到这一点。但我的主要问题是我不知道如何在腌制
对象时定义步骤。你是如何首先创建对象的?它会返回一个值吗?我试过了:
pickler = pickle.Pickler(数据)
pickle.Pickler(数据)
然后我试过了:
pickler .dump(图片)
data.dump(图片)
基本上,我只是对所涉及的所有语法感到困惑。我操作哪些
对象
Here''s what I have:
import pickle
data = open(r''C:\pickle_data.txt'', ''w'')
image = open(r''C:\peakhell.jpg'')
pickle.Pickler(data)
data.dump(image)
data.close()
image.close()
First off, I''m not sure the second line is the way to do that. But my
main problem is I don''t know how to define the steps when pickling an
object. How do you first create the object? Does it return a value? I tried:
pickler = pickle.Pickler(data)
pickle.Pickler(data)
Then I tried:
pickler.dump(image)
data.dump(image)
Basically, I''m just confused about all the syntax involved. Which
objects do I operate on
任何支持酸洗的对象。
文件句柄不属于那个小组(你有什么期望
pickle与文件句柄有关?)
或者是对象参考Pickler实例?你真的不需要这么做; b $ b b b b b b b b b b b b b b b b b b b只需使用dump * function *:
pickle.dump(对象,文件)
但如果你坚持使用你自己的Pickler例如,你必须将pickler实例保存在一个变量中,然后调用dump方法,在$ / b
的立场中:
myfile = open(somefilename," wb")
mypickler = pickle.Pickler(myfile)
mypickler.dump(object)
>
< / F>
any object that supports pickling.
file handles does not belong to that group (what did you expect
pickle to do with a file handle ?)
or did "object" refer to the Pickler instance? you don''t really need to
bother with that; just use the dump *function* instead:
pickle.dump(object, file)
but if you insist on using your own Pickler instance, you have to save
the pickler instance in a variable, and call the dump method on that in-
stance:
myfile = open(somefilename, "wb")
mypickler = pickle.Pickler(myfile)
mypickler.dump(object)
</F>
Fredrik Lundh写道:
Fredrik Lundh wrote:
文件句柄不属于那个小组(你期望用文件句柄做什么?)
或者做了对象参考Pickler实例?你真的不需要为此烦恼;只需使用dump * function *:
pickle.dump(对象,文件)
但如果你坚持使用你自己的Pickler实例,你必须保存
变量中的pickler实例,并在其中调用dump方法:
myfile = open(somefilename," wb")
mypickler = pickle .Pickler(myfile)
mypickler.dump(对象)
file handles does not belong to that group (what did you expect
pickle to do with a file handle ?)
or did "object" refer to the Pickler instance? you don''t really need to
bother with that; just use the dump *function* instead:
pickle.dump(object, file)
but if you insist on using your own Pickler instance, you have to save
the pickler instance in a variable, and call the dump method on that in-
stance:
myfile = open(somefilename, "wb")
mypickler = pickle.Pickler(myfile)
mypickler.dump(object)
对不起,但我非常困惑。似乎什么都没有用于我/ b $ b我。我认为*我需要挑选的是一个图像文件,但到目前为止我认为
所有我正在做的是将文件句柄作为''对象'传递,其中
可能是不正确的。我如何让实际的图像对象通过
in?但是我也试过腌制一根绳子而且也没用。
我尝试了两种你建议的方式,但是我不能错过这些错误。我
继续获得
AttributeError:''module''对象没有属性''dump''(当我使用
$时) b $ b函数)
或
AttributeError:''module''对象没有属性''dump''(当我创建时)
一个实例)
我需要做些什么才能使用pickle模块?我的
程序似乎认识到导入,但它一直说没有什么
是它的一个属性。
I''m sorry, but I''m terribly confused. Nothing seems to be working for
me. I *think* what I need to pickle is an image file, but so far I think
all I''m doing is passing the file handle in as the ''object'', which
probably isn''t correct. How would I get the actual image object to pass
in? But I also tried pickling a string and that didn''t work either.
I tried it both ways you suggest, but I can''t get past the errors. I
keep getting either
AttributeError: ''module'' object has no attribute ''dump'' (when I use the
function)
or
AttributeError: ''module'' object has no attribute ''dump'' (when I create
an instance)
Is there something special I need to do to use the pickle module? My
program seems to recognize the import, but it keeps saying that nothing
is an attribute of it.
John Salerno写道:
John Salerno wrote:
AttributeError:''module''对象没有属性''dump''(当我创建一个实例时)
AttributeError: ''module'' object has no attribute ''dump'' (when I create
an instance)
应该是:
AttributeError:''module''对象没有属性''Pickler''(当我
创建一个实例)
That should be:
AttributeError: ''module'' object has no attribute ''Pickler'' (when I
create an instance)
这篇关于你怎么用泡菜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!