【发布时间】:2017-08-25 16:06:06
【问题描述】:
这是我输入学生详细信息的代码。用户输入详细信息并输入“是”后,详细信息将导出到 StudentDetails.csv (Microsoft Excel),它应该位于标题下方,但最终会转到其他位置。
def EnterStudent():
uchoice_loop = False
ask_loop = False
while uchoice_loop == False:
surname = raw_input("What is the surname?")
forename = raw_input("What is the forname?")
date = raw_input("What is the date of birth? {Put it in the format D/M/Y}")
home_address = raw_input("What is the home address?")
home_phone = raw_input("What is the home phone?")
gender = raw_input("What is their gender?")
tutor_group = raw_input("What is their tutor group?")
email = (forename.lower() + surname.lower() + ("@school.com"))
print(surname+" "+forename+" "+date+" "+home_address+" "+home_phone+" "+gender+" "+tutor_group+" "+email)
ask = raw_input("Are these details correct?"+"\n"+"Press b to go back, or yes to add entered data on your student.").lower()
if ask == "yes":
f = open("StudentDetails.csv","rt")
lines = f.readlines()
f.close()
lines.append(surname+","+forename+","+date+","+home_address+","+home_phone+","+gender+","+tutor_group+","+email+"\n")
f = open("StudentDetails.csv", "w")
f.writelines(lines)
f.close()
uchoice_loop = True
printMenu()
elif ask == "b":
uchoice_loop = False
else:
print("Plesase enter 'b' to go back or 'yes' to continue")
这是我的 csv 文件。 enter image description here
【问题讨论】:
-
欢迎来到 Stack Overflow!请编辑您的帖子以提供更多信息。相反,数据会去哪里?您收到哪些错误消息?这将帮助我们帮助您。
标签: python python-2.7 csv