英文:
How to encode a dictionary then write it to a file then decode it to then set it as variables?
问题
以下是要翻译的内容:
所以,我正在创建一个游戏,其中有一些变量,比如:他们是否有x纸张,他们是否完成了这个房间,他们的物品清单中有什么。如下所示:
Code1
然后,假设我重新启动游戏,它会检查文件中是否有任何数据,如果没有,它会创建变量(这不重要),但如果有数据,它将加载它们,如下所示:
Code2
这是json文件的内容:
{"hd1": true, "hd2": true, "hd3": false, "hd4": false, "hd5": false, "P1": "Unlocked", "P2": "Unlocked", "P3": "Unlocked", "P4": "Locked", "P5": "Locked", "Inv": ["Killed my wife, my name is John. \n John created the safe // The code to the SAFE is 728 and the code to the LOCK is 1538 // Born on the 6th of November", "If you have found this letter, good, help me, I think I have gone south to the nearby yellow tree, PLEASE HELP!! \n Suit of Sir John the Great // The sky seems blue today, I'm wearing a red coat and some cargo green trousers, the sun looks more yellow today too"]}
理论上,我想要做的是将这些数据编码为base64,然后解码它,然后将其读取为json,然后重新加载保存的数据。
以下是简明的表达方式:
变量编码 --> 存储在文件中 --> 读取并解码 --> 加载变量
注意:我有一个名为"Inventory"的列表。
英文:
So, I am creating a game where there are some variables like: Have they got x paper, have they done this room, what they have in their inventory. As seen here:
Code1
And then lets say I then restart the game, it checks if there is any data in the file, if not it creates the variables (this isn't important) but if so it will load them as seen here:
Code2
And this is what the json file looks like:
{"hd1": true, "hd2": true, "hd3": false, "hd4": false, "hd5": false, "P1": "Unlocked", "P2": "Unlocked", "P3": "Unlocked", "P4": "Locked", "P5": "Locked", "Inv": ["Killed my wife, my name is John. \n John created the safe // The code to the SAFE is 728 and the code to the LOCK is 1538 // Born on the 6th of November", "If you have found this letter, good, help me, I think I have gone south to the nearby yellow tree, PLEASE HELP!! \n Suit of Sir John the Great // The sky seems blue today, I'm wearing a red coat and some cargo green trousers, the sun looks more yellow today too"]}
In theory, what I want to do is somehow encode this in base64, then decode it to then read it as a json to then reload the save data.
Here's a concise way of putting it:
Variables encoded --> store in file --> Reads and decodes it --> loads the variables
NOTE: I do have a list called "Inventory" as seen.
答案1
得分: 0
使用base64
库以及json.dump
和json.load
的字符串变体。
import base64, json
def save():
dictionary = { ... }
with open("save.dat", "wb") as outfile:
outfile.write(base64.b64encode(json.dumps(dictionary).encode()))
当您重新加载时,使用以下代码:
with open("save.dat", "rb") as infile:
dictionary = json.loads(base64.b64decode(infile.read()))
英文:
Use the base64
library along with the string variants of json.dump
and json.load
.
import base64, json
def save():
dictionary = { ... }
with open("save.dat", "wb") as outfile:
outfile.write(base64.b64encode(json.dumps(dictionary).encode()))
When you reload it you use:
with open("save.dat", "rb") as infile:
dictionary = json.loads(base64.b64decode(infile.read()))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论