我编写此代码是为了将列表的内容写入文本文件
def writefile(daymonths):
filename = "/Users/admin/Documents/Month.txt"
outputfile = open(filename, "w")
months = ["{}\n".format(month) for month in daymonths]
outputfile.writelines(months)
outputfile.close()
但该文件只显示最后一项,我尝试使用outputfile.write(),但仍然没有显示整个列表。 文本文件屏幕截图如下
代码中存在缩进错误。如果
writelines()
不起作用,则尝试使用for循环。写入时间为u,以月为单位,outputfile.Write(u)。您的代码:相关问题 更多 >
编程相关推荐