我有一个名为studentDetailsCopy
的CSV文件,需要在它的末尾添加一行数据,但此时它将它添加到最后一行的末尾,所以结果如下:电子邮件末尾的a
和{
这是我的代码:
newStudentAttributes = ([str(lastRowInt), newSurname, newForename, newDoB, newAddress, newHomePhoneNumber, newGender, newTutorGroup, newSchoolEmail])
with open('studentDetailsCopy.csv', 'a') as studentDetailsCSV:
writer = csv.writer(studentDetailsCSV, dialect='excel')
writer.writerow(newStudentAttributes)
当您使用open(file,“a”)时,python将始终打开到文件的末尾。由于您的CSV文件底部没有空换行“\r\n”,即最后一行是“26,…”,CSV writer会附加到该行。在这个循环中,您应该使用open(file,“a+”)读取最后一行,检查它是否为空。如果不是空的话就做作家.作家()插入新行。在
也许试着把
newStudentAttributes
上的括号去掉?在相关问题 更多 >
编程相关推荐