我的程序无法正常工作。我有根据用户输入创建的2D列表。在所有用户输入之后,应该将2D列表写入外部文件,这给我一个错误。
outfile = open("food.txt","a")
for foo in menu:
outfile.write(",".join(foo) + "\n")
outfile.close()
答案 0 :(得分:0)
您尝试创建新文件吗?
outfile = open("food.txt","w")
编辑:
请分享有关如何创建列表的更多信息。
您的代码通过一维字符串列表正确运行:
menu = ['apple', 'banana', 'cucumber']
outfile = open("food.txt","a")
for foo in menu:
outfile.write(",".join(foo) + "\n")
outfile.close()
food.txt
的内容:
a,p,p,l,e
b,a,n,a,n,a
c,u,c,u,m,b,e,r