Python 存储数据 [英] Python Storing Data

查看:27
本文介绍了Python 存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个列表.我有一个函数可以附加到列表中,不幸的是,当您关闭程序时,您添加的内容消失了,列表又回到了开头.有什么方法可以存储数据,以便用户可以重新打开程序并且列表已满.

I have a list in my program. I have a function to append to the list, unfortunately when you close the program the thing you added goes away and the list goes back to the beginning. Is there any way that I can store the data so the user can re-open the program and the list is at its full.

推荐答案

你可以制作一个数据库并保存它们,唯一的方法就是这个.带有 SQLITE 或 .txt 文件的数据库.例如:

You can make a database and save them, the only way is this. A database with SQLITE or a .txt file. For example:

with open("mylist.txt","w") as f: #in write mode
    f.write("{}".format(mylist))

您的列表进入 format() 函数.它将创建一个名为 mylist 的 .txt 文件,并将您的列表数据保存到其中.

Your list goes into the format() function. It'll make a .txt file named mylist and will save your list data into it.

之后,当您想再次访问您的数据时,您可以:

After that, when you want to access your data again, you can do:

with open("mylist.txt") as f: #in read mode, not in write mode, careful
    rd=f.readlines()
print (rd)

这篇关于Python 存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆