2017-08-25 16 views
0

これは、学生の詳細を入力するためのコードです。ユーザが詳細を入力して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

ようこそスタックオーバーフロー!少し詳しい情報を提供するために投稿を編集してください。代わりにデータはどこに行くのですか?どのようなエラーメッセージが表示されますか?これは私たちがあなたを助けるのに役立ちます。 – meenaparam

答えて

0

これを行うにはいくつかのことがあります。あなたは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 
0

エリックはなくcumbersomely読書の(https://docs.python.org/3.6/library/functions.html#openを参照してください)あなたが最良の追記モードでファイルを開くことで、右である以下の例のようにStudentDetails.csvそれを追加しますあなたのファイルを何度も何度も書き直します。

これは、標準ライブラリのcsvモジュール(https://docs.python.org/3.6/library/csv.htmlを参照)を使用して楽しむことができます。後でExcelで出力ファイルを使用する場合は特にそうです。

whileループ条件文で変数を使用せず、continuebreak文について学習することをお勧めします。この例の外側のループから抜け出したい場合は、tryexceptおよびraiseを調べてください。あなたが本当にPythonの2.xのを使用する必要がない限り

最後に、私は、Python 3以下のPython 3で書かれたコードを使用して起動することをお勧めいたしますと

#!/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() 

ベストのPython 2には動作しません。運の!

関連する問題