大家好,我正在創建一個游戲,允許用戶將他們的進度匯出到另一個檔案,一旦他們回來就可以再次加載該檔案。我想知道是否有辦法將多個變數匯出到不同的檔案,然后更改計算機記憶體中的檔案。我有一種匯入變數的方法,我只需要一些關于匯出部分的幫助,謝謝你的幫助,Darren。
uj5u.com熱心網友回復:
您可以嘗試將用戶進度資料存盤在 JSON 檔案中。在 python 中它非常簡單,你只需要使用json庫。
首先你應該匯入 lib
import json
例如玩家的資料是這樣的
player_data = {
'username': 'Nemo',
'xp': 1000,
'armor': {
'name': 'Kaer Morhen armor',
'weight': 1.57
}
}
比您可以輕松地將這些資料匯出到 JSON 檔案
with open("data_file.json", "w") as wf:
json.dump(player_data, wf)
并將其匯入回來
with open("data_file.json", "r") as rf:
player_data = json.load(rf)
我希望它對你有幫助:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/437211.html
上一篇:在Ruby中,為什么插入分配給陣列索引的變數回傳未定義?
下一篇:計算距離列均值的新資料框變數
