如何将用户输入的数据显示在标题下方

问题描述:

这是我输入学生详细信息的代码。一旦用户输入了详细信息并输入yes,详细信息将导出到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

+0

欢迎来到Stack Overflow!请修改您的帖子以提供更多信息。数据在哪里呢?你收到什么错误信息?这将帮助我们帮助你。 – meenaparam

有几件事你可以做,使这项工作。你不需要打开StudentDetails.csv并阅读所有的行。相反,你可以做一个行字符串变量并添加它的StudentDetails.csv就像在下面的例子中

#f = open("StudentDetails.csv","rt") 
#lines = f.readlines() 
#f.close() 
lines = surname+","+forename+","+date+","+home_address+","+home_phone+","+gender+","+tutor_group+","+email 

# the "a" appends the lines variable to the csv file instead of writing over it like the "w" does 
f = open("StudentDetails.csv", "a") 
f.writelines(lines) 
f.close() 
uchoice_loop = True 

埃里克是正确的,你最好打开附加模式的文件(见https://docs.python.org/3.6/library/functions.html#open),而不是麻烦地阅读并一遍又一遍重写你的文件。

我想补充一点,您可能会喜欢使用标准库的csv模块(请参阅https://docs.python.org/3.6/library/csv.html),特别是如果您想在之后在Excel中使用您的输出文件。

然后,我还建议您不要在while循环条件中使用变量,而是要了解continuebreak语句。如果您想在示例中跳出外部循环,请研究try,exceptraise

最后,除非你真的有使用Python 2.x中,我建议你开始使用Python 3,下面的代码是用Python编写3并不会在Python 2

#!/usr/bin/env python 
# -*- codig: utf-8 -*- 

import csv 


def enterStudent(): 
    b_or_yes = 'Press b to go back, or yes to save the entered data: ' 
    while True: 
     surname = input('What is the surname? ') 
     forename = input('What is the first name? ') 
     date = input(
      'What is the date of birth? {Put it in the format D/M/Y} ') 
     home_address = input('What is the home address? ') 
     home_phone = input('What is the home phone? ') 
     gender = input('What is the gender? ') 
     tutor_group = input('What is the tutor group? ') 
     email = forename.lower() + surname.lower() + '@school.com' 
     studentdata = (
      surname, 
      forename, 
      date, 
      home_address, 
      home_phone, 
      gender, 
      tutor_group, 
      email) 
     print(studentdata) 
     while True: 
      reply = input('Are these details correct?\n' + b_or_yes).lower() 
      if reply == 'yes': 
       with open('studentdetails.csv', 'a', newline='') as csvfile: 
        studentwriter = csv.writer(csvfile, dialect='excel') 
        studentwriter.writerow(studentdata) 
       break 
      elif reply == 'b': 
       break 


if __name__ == '__main__': 
    enterStudent() 

最好的工作好运!