用python腌制的泡菜 [英] In a pickle with pickling in python

查看:81
本文介绍了用python腌制的泡菜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览过该网站和许多其他网站,但似乎没有人给我最简单的答案.在纸浆波纹管中,需要将2个不同的变量放入单个泡菜中(又称为"test1"和"test2").但我完全无法获得两者中较简单的一个.没有错误消息或其他任何消息,并且确实向咸菜中写入了一些东西,但随后我关闭了程序",重新打开它,尝试加载咸菜,但"test1"的值没有改变. /p>

第二个问题是如何将两者保存到相同的泡菜中?起初,我尝试使用allStuff变量存储test1和test2,然后转储allStuff ...转储似乎是成功的,但加载并没有完成.香港专业教育学院尝试了一种变体,其中列出了每个应加载的文件,但这只会导致很多错误,并导致我殴打我那可怜的旧键盘...

请帮助.

import pickle

class testing():

    test1 = 1000
    test2 = {'Dogs' : 0, 'Cats' : 0, 'Birds' : 0, 'Mive' : 0}


    def saveload():
            check = int(input(' 1. Save  :  2. Load  : 3. Print  : 4. Add'))
            allStuff = testing.test1, testing.test2
            saveFile = 'TestingSaveLoad.data'

            if check == 1:
                f = open(saveFile, 'wb')
                pickle.dump(testing.test1, f)
                f.close()
                print()
                print('Saved.')
                testing.saveload()


            elif check == 2:
                f = open(saveFile, 'rb')
                pickle.load(f)
                print()
                print('Loaded.')
                testing.saveload()        


            elif check == 3:
                print(allStuff)
                testing.saveload()

            else:
                testing.test1 += 234
                testing.saveload()


testing.saveload()

解决方案

pickle.load 文档状态:

从打开的文件对象文件中读取一个腌制的对象表示形式,并返回其中指定的重构对象层次结构.

因此,您将需要以下内容:

testing.test1 = pickle.load(f)

但是,要保存和加载多个对象,可以使用

# to save
pickle.dump(allStuff, f)

# to load
allStuff = pickle.load(f)
testing.test1, testing.test2 = allStuff

I have gone through this website and many others but no one seems to give me the simplest possible answer. In the scrip bellow there are 2 different variables that need to be placed into a single pickle (aka 'test1' and 'test2'); but I am wholly unable to get even the simpler one of the two to load. There are no error messages or anything, and it does appear that something is being written to the pickle but then I close the 'program', re open it, try to load the pickle but the value of 'test1' does not change.

The second question is how to save both to the same pickle? at first i tried using the allStuff variable to store both test1 and test2 then dumping allStuff...the dump seems to be a success but loading does jack. Ive tried a variation where you list each file that should be loaded but this just caused a whole lot of errors and caused me to assault my poor old keyboard...

Please Help.

import pickle

class testing():

    test1 = 1000
    test2 = {'Dogs' : 0, 'Cats' : 0, 'Birds' : 0, 'Mive' : 0}


    def saveload():
            check = int(input(' 1. Save  :  2. Load  : 3. Print  : 4. Add'))
            allStuff = testing.test1, testing.test2
            saveFile = 'TestingSaveLoad.data'

            if check == 1:
                f = open(saveFile, 'wb')
                pickle.dump(testing.test1, f)
                f.close()
                print()
                print('Saved.')
                testing.saveload()


            elif check == 2:
                f = open(saveFile, 'rb')
                pickle.load(f)
                print()
                print('Loaded.')
                testing.saveload()        


            elif check == 3:
                print(allStuff)
                testing.saveload()

            else:
                testing.test1 += 234
                testing.saveload()


testing.saveload()

解决方案

The pickle.load documentation states:

Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein.

So you would need something like this:

testing.test1 = pickle.load(f)

However, to save and load multiple objects, you can use

# to save
pickle.dump(allStuff, f)

# to load
allStuff = pickle.load(f)
testing.test1, testing.test2 = allStuff

这篇关于用python腌制的泡菜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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