繁体   English   中英

如何用 Pickle 存储自我价值?

[英]How to store self values with Pickle?

class Player:
    def __init__(self):
        self.money = 0
        self.level = 0
        self.xp = 0
        self.xp_until_level = 20
        self.taxi_car = "Cabbie"
        self.busines = "Нет"
        self.busines_income = 1000
        self.upgrade_cost = 10000
        self.car_speed = 10
        self.car_level = 0
        self.drives_until_new_car = 20

p = Player()

我不知道如何保存self价值。 我制作文字游戏并保存数据是我的第一个问题。

self 值是您的实例的属性。 只需腌制您的实例(此处为p )并存储所有属性:

import pickle

class Player:
    def __init__(self):
        self.money = 0
        self.level = 0
        self.xp = 0
        self.xp_until_level = 20
        self.taxi_car = "Cabbie"
        self.busines = "Нет"
        self.busines_income = 1000
        self.upgrade_cost = 10000
        self.car_speed = 10
        self.car_level = 0
        self.drives_until_new_car = 20

p = Player()

pickled = pickle.dumps(p)
new_obj = pickle.loads(pickled)
print(new_obj.taxi_car)  # Cabbie

将 class 实例存储到二进制文件中:

class 实例(或任何其他对象)可以存储到腌制(序列化)二进制文件或从中读取,以供以后使用或存档。

例如:

import pickle

# Create a class instance (as you have done).    
p = Player()

# Store the class instance to a file.
with open('./cabbie.p', 'wb') as f:  # <-- Note the 'wb' mode, for write-binary
    pickle.dump(p, f)
    
# Read the class instance from a file.
with open('./cabbie.p', 'rb') as f:  # <-- Note the 'rb' mode, for read-binary
    data = pickle.load(f)
    

测试:

>>> vars(data)

{'money': 0,
 'level': 0,
 'xp': 0,
 'xp_until_level': 20,
 'taxi_car': 'Cabbie',
 'busines': 'Нет',
 'busines_income': 1000,
 'upgrade_cost': 10000,
 'car_speed': 10,
 'car_level': 0,
 'drives_until_new_car': 20}

泡菜当然是一种选择。 但您也可以考虑使用ConfigParser 它内置于标准库中,因此您不必安装另一个 package。 ConfigParser 的优点是您可以获得易于人类阅读的漂亮 ini 样式文件。 这是一些代码:

import configparser

class Player:
    def __init__(self):
        self.money = 0
        self.level = 0
        self.xp = 0
        self.xp_until_level = 20
        self.taxi_car = "Cabbie"
        self.busines = "Het"
        self.busines_income = 1000
        self.upgrade_cost = 10000
        self.car_speed = 10
        self.car_level = 0
        self.drives_until_new_car = 20

        self.config=configparser.ConfigParser()

    def save_config(self):
        self.config['default'] = {}   # make blank section 'default'
        self.config['default']['money']                = f'{self.money}'
        self.config['default']['level']                = f'{self.level}'
        self.config['default']['xp']                   = f'{self.xp}'
        self.config['default']['xp_until_level']       = f'{self.xp_until_level}'
        self.config['default']['taxi_car']             = self.taxi_car
        self.config['default']['busines']              = self.busines
        self.config['default']['busines_income']       = f'{self.busines_income}'
        self.config['default']['upgrade_cost']         = f'{self.upgrade_cost}'
        self.config['default']['car_speed']            = f'{self.car_speed}'
        self.config['default']['car_level']            = f'{self.car_level}'
        self.config['default']['drives_until_new_car'] = f'{self.drives_until_new_car}'
        with open('settings.ini', 'w') as fid:
            self.config.write(fid)

    def load_config(self):
        self.config.read('settings.ini')
        self.money                = int(self.config['default']['money'])
        self.level                = int(self.config['default']['level'])
        self.xp                   = int(self.config['default']['xp'])
        self.xp_until_level       = int(self.config['default']['xp_until_level'])
        self.taxi_car             = self.config['default']['taxi_car']
        self.busines              = self.config['default']['busines']
        self.busines_income       = int(self.config['default']['busines_income'])
        self.upgrade_cost         = int(self.config['default']['upgrade_cost'])
        self.car_speed            = int(self.config['default']['car_speed'])
        self.car_level            = int(self.config['default']['car_level'])
        self.drives_until_new_car = int(self.config['default']['drives_until_new_car'])

p = Player()
p.save_config()
p.load_config()
print(dict(p.config['default']))

将数据加载/保存到变量中有很多额外的开销。 这是settings.ini文件的样子:

[default]
money = 0
level = 0
xp = 0
xp_until_level = 20
taxi_car = Cabbie
busines = Het
busines_income = 1000
upgrade_cost = 10000
car_speed = 10
car_level = 0
drives_until_new_car = 20

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM