Python 如何在csv文件的单个单元格中写入,而不是在单独的单元格中写入?

Python 如何在csv文件的单个单元格中写入,而不是在单独的单元格中写入?,python,csv,Python,Csv,这是我的密码。当我运行它时,它会在每个单元格中打印学生的名字和一个字母(比如B | o | B而不是Bob)。电子邮件也是如此。有人能帮我一个简单的忙吗:我有点像python的noob。writerow接受列表作为参数,并将每个元素写入单元格。因为字符串是Python中的字符列表,所以每个字母都存储在一个单元格中。您应该这样做: import csv with open ("students.csv", "a") as newFile: newFileWriter = csv.

这是我的密码。当我运行它时,它会在每个单元格中打印学生的名字和一个字母(比如B | o | B而不是Bob)。电子邮件也是如此。有人能帮我一个简单的忙吗:我有点像python的noob。

writerow
接受列表作为参数,并将每个元素写入单元格。因为字符串是Python中的字符列表,所以每个字母都存储在一个单元格中。您应该这样做:

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)

希望有帮助

writerow只接受列表参数

在一行中添加学生姓名,在另一行中添加学生电子邮件

newFileWriter.writerow([student, student_email])
如果你想让它在同一排

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