本文介绍了你怎么用泡菜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这就是我所拥有的: 导入泡菜 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 pickledata = 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 mymain problem is I don''t know how to define the steps when pickling anobject. 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. Whichobjects do I operate on, and when? I can''t find these answers in thedocumentation 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\MyDocuments\Python24\myscripts\challenges\pickle.py" , line 3, in -toplevel-import pickleFile "C:\Documents and Settings\jsaler01.TUFTS\MyDocuments\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 totallywrong if it can''t even get past the initial creation of a pickler object.推荐答案 任何支持酸洗的对象。 文件句柄不属于那个小组(你有什么期望 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 expectpickle to do with a file handle ?)or did "object" refer to the Pickler instance? you don''t really need tobother 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 savethe 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> 对不起,但我非常困惑。似乎什么都没有用于我/ 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 forme. I *think* what I need to pickle is an image file, but so far I thinkall I''m doing is passing the file handle in as the ''object'', whichprobably isn''t correct. How would I get the actual image object to passin? 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. Ikeep getting eitherAttributeError: ''module'' object has no attribute ''dump'' (when I use thefunction)orAttributeError: ''module'' object has no attribute ''dump'' (when I createan instance)Is there something special I need to do to use the pickle module? Myprogram seems to recognize the import, but it keeps saying that nothingis an attribute of it. 应该是: AttributeError:''module''对象没有属性''Pickler''(当我 创建一个实例)That should be:AttributeError: ''module'' object has no attribute ''Pickler'' (when Icreate an instance) 这篇关于你怎么用泡菜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-04 07:41