所以我在做一个ToDo应用程序,我需要将一组句子和单词保存到一个.txt文件中。我做了一些研究,但还没有找到任何教程,解释它足够好,所以我可以理解它。正如我所说,我使用的是Python3。代码如下:
# Command line TO-DO list
userInput = None
userInput2 = None
userInput3 = None
todo = []
programIsRunning = True
print("Welcome to the TODO list made by Alex Chadwick. Have in mind
that closing the program will result in your TODO"
" list to DISAPPEAR. We are working on correcting that.")
print("Available commands: add (will add item to your list); remove
(will remove item from your list); viewTODO (will"
" show you your TODO list); close (will close the app")
with open('TODOList.txt', 'r+') as f:
while programIsRunning == True:
print("Type in your command: ")
userInput = input("")
if userInput == "add":
print("Enter your item")
userInput2 = input("")
todo.append(userInput2)
continue
elif userInput == "viewTODO":
print(todo)
continue
elif userInput == "remove":
print(todo)
userInput3 = input("")
userInput3 = int(userInput3)
userInput3 -= 1
del todo[userInput3]
continue
elif userInput == "close":
print("Closing TODO list")
programIsRunning = False
continue
else:
print("That is not a valid command")
您可以使用一个简单的文本文件来存储待办事项并从中检索它们:
这听起来像是
pickle
的工作Pickle是用Python保存对象和数据的内置模块。要使用它,您需要将
import pickle
放在程序的顶部保存到文件:
从文件加载:
这个答案中的代码取自here.
我希望这有帮助
相关问题 更多 >
编程相关推荐