在代码运行之间保留变量的数据
Keeping the data of a variable between runs of code
为了一个学校项目,我正在 Python 中制作刽子手游戏。现在我的代码像这样从字典中挑选一个词:
WordList = ["cat", "hat", "jump", "house", "orange", "brick", "horse", "word"]
word = WordList[random.randint(0, len(WordList) - 1)]
现在必须在 运行 之前的代码中设置单词列表,但我添加了向列表添加单词的功能,同时 运行 it:
if command == "add":
while True:
print("type a word to add to the dictionary")
print("type /b to go back to game")
add = raw_input("word: ")
if add != "/b":
WordList = WordList + [add]
print add, "added!"
else:
print("returning to game")
break
然而,一旦我退出代码,添加的单词显然不会被保存,所以我要么必须手动将所有单词添加到列表中,要么每次代码开始时添加一堆单词到列表中时间。所以我想知道是否有一种简单的方法可以在代码完成后保存变量,这样 WordList 将在下次代码开始时保留添加的单词。我用来编写 python 的程序是 Jetbrains PyCharm,如果有区别的话。对于任何非最佳代码表示歉意,我是代码新手。
你必须使用持久存储:添加单词时将单词写入文件,程序启动时从该文件中检索它们。
如果您退出代码,您将停止进程。因此,您会丢失所有数据。您必须添加使脚本保持活动状态的字样。建议使用一个服务器来处理您的所有呼叫(例如:http://flask.pocoo.org/) or to use the python command input (https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output)。
但请记住...如果您停止该过程,您将丢失所有数据,这是正常的。
否则,在停止脚本之前,您必须将所有数据保存到文件或数据库中,并在脚本启动时加载它们。
简单地挑选你想要保持持久的数据。由于您的用例不需要非常复杂的数据存储,因此 pickling 是一个很好的选择。一个小例子:
import pickle
word_list = ["cat", "hat", "jump", "house", "orange", "brick", "horse", "word"]
# do your thing here, like
word_list.append("monty")
# open a pickle file
filename = 'mypickle.pk'
with open(filename, 'wb') as fi:
# dump your data into the file
pickle.dump(word_list, fi)
以后需要再次使用时,加载即可:
# load your data back to memory when you need it
with open(filename, 'rb') as fi:
word_list = pickle.load(fi)
哒哒!您现在拥有数据持久性。多读书here。一些重要的提示:
- 当我使用
open()
打开文件时请注意 'b'
。 Pickles 通常以二进制格式存储,因此您必须以二进制模式打开文件。
- 我使用了
with
上下文管理器。这可确保在我对文件的所有工作完成后安全关闭文件。
为了一个学校项目,我正在 Python 中制作刽子手游戏。现在我的代码像这样从字典中挑选一个词:
WordList = ["cat", "hat", "jump", "house", "orange", "brick", "horse", "word"]
word = WordList[random.randint(0, len(WordList) - 1)]
现在必须在 运行 之前的代码中设置单词列表,但我添加了向列表添加单词的功能,同时 运行 it:
if command == "add":
while True:
print("type a word to add to the dictionary")
print("type /b to go back to game")
add = raw_input("word: ")
if add != "/b":
WordList = WordList + [add]
print add, "added!"
else:
print("returning to game")
break
然而,一旦我退出代码,添加的单词显然不会被保存,所以我要么必须手动将所有单词添加到列表中,要么每次代码开始时添加一堆单词到列表中时间。所以我想知道是否有一种简单的方法可以在代码完成后保存变量,这样 WordList 将在下次代码开始时保留添加的单词。我用来编写 python 的程序是 Jetbrains PyCharm,如果有区别的话。对于任何非最佳代码表示歉意,我是代码新手。
你必须使用持久存储:添加单词时将单词写入文件,程序启动时从该文件中检索它们。
如果您退出代码,您将停止进程。因此,您会丢失所有数据。您必须添加使脚本保持活动状态的字样。建议使用一个服务器来处理您的所有呼叫(例如:http://flask.pocoo.org/) or to use the python command input (https://en.wikibooks.org/wiki/Python_Programming/Input_and_Output)。
但请记住...如果您停止该过程,您将丢失所有数据,这是正常的。
否则,在停止脚本之前,您必须将所有数据保存到文件或数据库中,并在脚本启动时加载它们。
简单地挑选你想要保持持久的数据。由于您的用例不需要非常复杂的数据存储,因此 pickling 是一个很好的选择。一个小例子:
import pickle
word_list = ["cat", "hat", "jump", "house", "orange", "brick", "horse", "word"]
# do your thing here, like
word_list.append("monty")
# open a pickle file
filename = 'mypickle.pk'
with open(filename, 'wb') as fi:
# dump your data into the file
pickle.dump(word_list, fi)
以后需要再次使用时,加载即可:
# load your data back to memory when you need it
with open(filename, 'rb') as fi:
word_list = pickle.load(fi)
哒哒!您现在拥有数据持久性。多读书here。一些重要的提示:
- 当我使用
open()
打开文件时请注意'b'
。 Pickles 通常以二进制格式存储,因此您必须以二进制模式打开文件。 - 我使用了
with
上下文管理器。这可确保在我对文件的所有工作完成后安全关闭文件。