首页 > 解决方案 > 有没有办法将用户输入保存到一个永久列表中,即使在 python 程序关闭后也会保存该列表

问题描述

我想在 python 3 中制作一个可以自学的聊天机器人。我希望它能够存储来自用户输入的数据。我怎么能做到这一点并将它保存在一个列表中,所以每当我重新检查它时,它都会存储来自不同用户的新变量。

标签: pythonpython-3.xchatbot

解决方案


将数据存储在文件中。了解读取和写入文件。您可以使用它的一些代码:

# Saving the information
file = open("filename.txt","w")
file.write('|'.join(my_list))
file.close()
# To get the information just do 
file = open("filename.txt","r").read()
my_list = file.split("|")
# Magic you have your list back again!

这还有很多,所以你应该做一些关于读写文件的研究。

https://www.guru99.com/reading-and-writing-files-in-python.html


推荐阅读