Pickle variables and classes for save function in

2019-09-01 01:35发布

I am trying to make a save function in a text adventure. The function must save some variables and parameters for the player.

I have tried to Google it and asking on forums, but I can't make it work.

The entire code is here: http://pastebin.com/8Y3PNnRx

The saving function should save the player = player('Niels', 'Knight, 0, 0, 0, 0, 0, 0, 1) (line 417) and variables like player.gold and player.potion (line 420) at location 'C:\\Users\\XXXXX\\Desktop\\SmallDungeons\\save.dat'

Thank you for your time!

标签: python pickle
2条回答
手持菜刀,她持情操
2楼-- · 2019-09-01 02:13

For a minimal example, we build a class in foo.py (this is similar to your class player).

# foo.py
class Foo(object):
  def __init__(self, x):
    self.x = x
  def bar(self):
    return self.x**2

Now we import it, and add some attributes (this is similar to you adding player.gold), and then pickle the class instance. When we're done, we terminate the python interpreter session.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo import Foo
>>> f = Foo(3)
>>> f.y = 10
>>> 
>>> import pickle
>>> with open('foobar.pkl', 'w') as pik:
...   pickle.dump(f, pik)
... 
>>> 

Then, we restart the session, and load the pickle.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> with open('foobar.pkl', 'r') as pik:
...   f = pickle.load(pik)
... 
>>> f.x
3
>>> f.bar()
9
>>> f.y
10
>>> 

You'll see that just by saving the class instance, even the added attributes (like f.y) are preserved. This is because pickle preserves a reference to your file (where to import it from) as well as any added attributes. You can see this in the pickle string itself.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo import Foo
>>> f = Foo(3)
>>> 
>>> import pickle
>>> pickle.dumps(f)
"ccopy_reg\n_reconstructor\np0\n(cfoo\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'x'\np6\nI3\nsb."
>>> 
>>> f.y = 10
>>> pickle.dumps(f)
"ccopy_reg\n_reconstructor\np0\n(cfoo\nFoo\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'y'\np6\nI10\nsS'x'\np7\nI3\nsb."

My suggestion is to use pickle protocol -1, as it makes the pickles smaller. You add this flag to any of the places you use dump or dumps.

>>> pickle.dumps(f, protocol=-1)
'\x80\x02cfoo\nFoo\nq\x00)\x81q\x01}q\x02U\x01xq\x03K\x03sb.'
查看更多
戒情不戒烟
3楼-- · 2019-09-01 02:32

You can save with:

pickle.dump((player, player.gold, player.potion), 'C:\\Users\\XXXXX\\Desktop\\SmallDungeons\\save.dat')

and load with

input2 = open('C:\\Users\\XXXXX\\Desktop\\SmallDungeons\\save.dat','r')
data = pickle.load(input2)
player = data[0]
player.gold = data[1]
player.potion = data[2]
查看更多
登录 后发表回答