Python保存列表更改(Python saving list changes)
list = []
while True:
list.append(input())
print(list)
这允许我在这个列表中添加我想要的任何内容。 但是,有没有办法可以保持对列表的更改,以便在我以后运行程序时,我之前编写的所有内容都将列在列表中?
编辑:如果重要,我使用PyCharm进行编码和运行我的程序
list = [] while True: list.append(input()) print(list)this allows me to add whatever I want to this list. However, Is there a way that I can keep the changes to the list so that when I run the program later all of the things I previously wrote will be on the list?
EDIT: If it matters, I use PyCharm for coding and running my progams
最满意答案
你可以在这里使用json 。
import json try: list = json.load(open("database.json")) except: list = [] while True: try: list.append(input()) print(list) except KeyboardInterrupt: json.dump(list, open('database.json', 'w'))这将内容存储在名为database.json的文件中,您可以使用CTRL+C结束程序
You can use json here.
import json try: list = json.load(open("database.json")) except: list = [] while True: try: list.append(input()) print(list) except KeyboardInterrupt: json.dump(list, open('database.json', 'w'))This stores the content in a file called database.json, you can end the program with CTRL+C
更多推荐
发布评论