我想知道如何保存用户输入的列表。我想知道如何将它保存到文件中。当我运行该程序时,它说我必须使用一个字符串来写入它。那么,有没有办法给文件分配一个列表,甚至每次运行程序时都会更好,它会自动更新文件列表?这将是伟大的文件将理想地是一个.txt。如何使用python将列表保存到文件?
stuffToDo = "Stuff To Do.txt"
WRITE = "a"
dayToDaylist = []
show = input("would you like to view the list yes or no")
if show == "yes":
print(dayToDaylist)
add = input("would you like to add anything to the list yes or no")
if add == "yes":
amount=int(input("how much stuff would you like to add"))
for number in range (amount):
stuff=input("what item would you like to add 1 at a time")
dayToDaylist.append(stuff)
remove = input("would you like to remove anything to the list yes or no")
if add == "yes":
amountRemoved=int(input("how much stuff would you like to remove"))
for numberremoved in range (amountRemoved):
stuffremoved=input("what item would you like to add 1 at a time")
dayToDaylist.remove(stuffremoved);
print(dayToDaylist)
file = open(stuffToDo,mode = WRITE)
file.write(dayToDaylist)
file.close()
你可以腌菜单 –
顺便说一下,它通常更易于“硬编码”模式参数打开(),而不是更少。使用这个伪常量只是令人困惑。此外,您不需要将其作为关键字参数提供;你可以把它叫做'open(“filename”,“a”)' – Schilcote