【发布时间】:2021-04-11 05:07:08
【问题描述】:
我是编码新手,正在做我的任务,我被困住了。提前致谢。
问题是
writeABFile()
该函数应打开“file.dat”以进行写访问 - 覆盖现有文件
'file'变量中的每一行都需要转换成如下格式的字符串:
8,8,8,8,8,8nl 8 代表椅子,如果椅子可用,则应设置为字符“0”(零),如果椅子已预订,则应设置为字符“1”。 'nl' 是“换行符/回车符”
使用 try 和 except 语句
file.dat 包含列表列表 - [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0 ,0],[0,0,0,0,0,0]]
我的代码是我认为我搞砸了:
def writeABFile():
while not opened:
try:
openFile = open("file.dat", "w")
for row in file:
openFile.write(",".join(map(str(8,8,8,8,8,8,), row)) + "\n")
openFile.close()
except Exception:
print("Failed to write to file.dat file")
else:
openFile.close()
【问题讨论】:
-
你到底有什么疑问?
-
我怀疑 openFile.write(",".join(map(str(8,8,8,8,8,8,), row)) + "\n")。它没有做我想要的。
标签: python python-3.x list