python - 有没有办法将用户输入保存到一个永久列表中,即使在 python 程序关闭后也会保存该列表
问题描述
我想在 python 3 中制作一个可以自学的聊天机器人。我希望它能够存储来自用户输入的数据。我怎么能做到这一点并将它保存在一个列表中,所以每当我重新检查它时,它都会存储来自不同用户的新变量。
解决方案
将数据存储在文件中。了解读取和写入文件。您可以使用它的一些代码:
# 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
推荐阅读
- c# - uwp 如何读取指定目录下的文件
- jenkins - 执行 TestNG 测试时出错,使用 Gradle 的 WebDriver 的 Cucumver 场景
- bash - 使用 bash 的部署脚本
- spring - 使用spring mvc处理thymeleaf中的一对多关系
- sql - Postgres查询以获取所有孩子的ID
- c++ - 安装期间未执行 CPP DLL CustomAction 代码
- go - systemd 日志游标如何工作?
- excel - SumIF + 索引 + 匹配公式
- ios - 如何使用 Alamofire 将参数发布到服务器?
- swift - 无法在 swift 4 中解决此 Null 问题?