我的主程序中激活了这个功能,目前将单个CSV文件保存到目录中效果很好,但如果需要,我希望允许用户存储和命名多个CSV文件。我试图启动文件名的输入,但在这样做时收到错误。目前正在使用csv模块。
def csvdata():
while True:
print("Press '1' to save student data to a CSV file, '2' to name and save a CSV file and '3' to return to the main menu.")
answer = int(input("Enter your option: "))
if answer == 1:
with open('student.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["Name","Grade"])
writer.writerows(master_list)
print("student.csv file saved")
elif answer == 2:
with open(input('Enter CSV file name:' '.csv', 'w', newline='')) as f:
writer = csv.writer(f)
writer.writerow(["Name", "Grade"])
writer.writerows(master_list)
elif answer == 3:
break
else:
print("Invalid option")```
correct:
with open(input('Enter CSV file name:' '.csv', 'w', newline='')) as f:
至
with open(input('Enter CSV file name:')+'.csv', 'w', newline='') as f: