2016-11-12 78 views
1
import csv 
with open ("students.csv", "a") as newFile: 
     newFileWriter = csv.writer(newFile) 
     student = input("Enter a student's name: ") 
     student_email = input("Enter a student's email: ") 
     newFileWriter.writerow(student) 
     newFileWriter.writerow(student_email) 

這是我的代碼。當我運行它時,它會在每個單元格中用一個字母打印學生的姓名(如B | o | b而不是Bob)。電子郵件也是一樣。有人可以幫助簡單地說:我是一個python noob。如何在單個單元格中編寫csv文件而不是單獨的單元格?

回答

2

writerow接受列表作爲參數並將每個元素寫入單元格中。由於字符串是Python中的字符列表,因此每個字母都存儲在單元格中。你應該這樣做:

newFileWriter.writerow([student, student_email]) 

希望它有幫助!

0

writerow只需要一個list參數

要在1行和學生的電子郵件中的另一行

newFileWriter.writerow([student]) 
newFileWriter.writerow([student_email]) 

增加學生的名字,如果你想它在同一行中

newFileWriter.writerow([student,student_email]) 
相關問題