Python保存列表更改(Python saving list changes)

编程入门 行业动态 更新时间:2024-10-26 08:21:23
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

更多推荐

本文发布于:2023-07-24 01:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240079.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Python   list   saving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!