gpt4 book ai didi

python - 架子写入空白列表/字典

转载 作者:行者123 更新时间:2023-11-30 22:43:50 27 4
gpt4 key购买 nike

我正在尝试创建一个 API 来在我制作的游戏上创建自定义 map ,但 shelve 只写入空白列表和字典。

我尝试这样存储它:“name”为 map 显示的名称“name_1”表示被视为变量的名称“textures”为纹理的文件路径“wind”表示吹的风量

这是我的代码:

import tkinter.filedialog as tfd
import shelve
import shutil as os

file1 = shelve.open("info")

if "written" in file1:
pass
else:
file1["name"] = []
file1["name_1"] = []
file1["textures"] = {}
file1["wind"] = {}
file1.close()
file1 = shelve.open("info")

name = input("Name: ")

name1 = input("Name zur Variablenspeicherung: ")

wind = input("Wind (*-*): ")

print("Wähle eine 1200x820 GIF-Datei als Hintergrund aus")
texture = tfd.askopenfilename()
os.copy(texture,"textures/"+texture.split("/")[-1].split("\\")[-1])
texture = "custom/textures/"+texture.split("/")[-1].split("\\")[-1]

print("Schreibe Datei...")

file1["name"].append(name)
file1["name_1"].append(name1)
file1["textures"][name1] = texture
file1["wind"][name1] = [int(wind.split("-")[0]),int(wind.split("-")[1])]
file1["written"] = 1

file1.close()

最佳答案

shelve 将在您设置该键时写出对键的更改。它无法检测值的变化。追加到列表或分配给字典上的键将不会被检测到。

来自shelve documentation :

Because of Python semantics, a shelf cannot know when a mutable persistent-dictionary entry is modified. By default modified objects are written only when assigned to the shelf (see Example). If the optional writeback parameter is set to True, all entries accessed are also cached in memory, and written back on sync() and close(); this can make it handier to mutate mutable entries in the persistent dictionary, but, if many entries are accessed, it can consume vast amounts of memory for the cache, and it can make the close operation very slow since all accessed entries are written back (there is no way to determine which accessed entries are mutable, nor which ones were actually mutated).

我的粗体强调。

writeback参数设置为True(并接受缺点):

file1 = shelve.open("info", writeback=True)

或显式分配回键:

names = file1["name"]
names.append(name)
file1["name"] = names

关于python - 架子写入空白列表/字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com