如何使用 "for" 循环搁置 save/load 数据?

How to save/load data with shelve using a "for" loop?

我正在尝试使用搁置 save/load 一些数据 to/from 文件。我有一个字典列表:

inv = [slot0, slot1, slot2, slot3, slot4, slot5, slot6]

这 7 个词典中的每一个看起来都是这样,但值略有不同:

slot0 = {"item_pos": [hud_x + 592, hud_y + 4], "text_pos": [hud_x + 612, hud_y + 25], "item": None, "amount": 0}

为了save/load字典,我使用了"shelve"模块,这里是保存代码:

with shelve.open((os.path.join(saves_path, "inventory", "inventory")), "c") as f:

        f["slot0_item"] = slot0["item"]
        f["slot0_amount"] = slot0["amount"]
        f["slot1_item"] = slot1["item"]
        f["slot1_amount"] = slot1["amount"]
        f["slot2_item"] = slot2["item"]
        f["slot2_amount"] = slot2["amount"]
        f["slot3_item"] = slot3["item"]
        f["slot3_amount"] = slot3["amount"]
        f["slot4_item"] = slot4["item"]
        f["slot4_amount"] = slot4["amount"]
        f["slot5_item"] = slot5["item"]
        f["slot5_amount"] = slot5["amount"]
        f["slot6_item"] = slot6["item"]
        f["slot6_amount"] = slot6["amount"]

加载代码如下:

with shelve.open((os.path.join(saves_path, "inventory", "inventory")), "c") as f:

        slot0["item"] = f["slot0_item"]
        slot0["amount"] = f["slot0_amount"]
        slot1["item"] = f["slot1_item"]
        slot1["amount"] = f["slot1_amount"]
        slot2["item"] = f["slot2_item"]
        slot2["amount"] = f["slot2_amount"]
        slot3["item"] = f["slot3_item"]
        slot3["amount"] = f["slot3_amount"]
        slot4["item"] = f["slot4_item"]
        slot4["amount"] = f["slot4_amount"]
        slot5["item"] = f["slot5_item"]
        slot5["amount"] = f["slot5_amount"]
        slot6["item"] = f["slot6_item"]
        slot6["amount"] = f["slot6_amount"]

虽然此代码工作正常,但它非常长且效率低下。我尝试使用 "for" 循环来保存数据,如下所示:

for slot in inv:
            f["slot_item"] = slot["item"]
            f["slot_amount"] = slot["amount"]

...并像这样加载数据:

for slot in inv:
            slot["item"] = f["slot_item"]
            slot["amount"] = f["slot_amount"]

但是,当我使用此方法时,词典不会 save/load 在重新启动程序时进行更改,就像它们应该的那样。我如何(如果可能的话)正确使用 "for" 循环来有效地 save/load 搁置数据?

编辑:hud_xhud_y 都等于 20

如果你能设法用字典或列表代替 slot1slot2 等,代码会容易得多。

import json
from pathlib import Path
import random

# repliacting some data 
def generate_dict():
   return {"item_pos": [rnd() + 592, rnd() + 4], 
           "text_pos": [rnd() + 612, rnd() + 25], 
           "item": None, 
           "amount": 0}

def rnd():
   return random.randint(0, 1000)    

# change of data structure
dict_list = [generate_dict() for id in range(7)]

# easily seriliasable into json
json_str = json.dumps(dict_list, sort_keys=True, indent=4)

# you can save json as file 
Path('store.json').write_text(json_str)

# read back
assert json.loads(Path('store.json').read_text()) == dict_list

我决定改用 pickle,因为它更容易与 "for" 循环一起使用。此外,shelve 使用类似字典的语法(pickle 不使用),因此这可能是问题的一部分。

节省:

with open((os.path.join(saves_path, "inventory", "inventory.txt")), "wb") as f:
        for slot in inv:
            pickle.dump(slot["item"], f)
            pickle.dump(slot["amount"], f)

正在加载:

with open((os.path.join(saves_path, "inventory", "inventory.txt")), "rb") as f:
        for slot in self.inventory.inv:
            slot["item"] = pickle.load(f)
            slot["amount"] = pickle.load(f)