写入CSV python时销毁空白单元格

我现在一直在做一个项目。 这个项目我正在打开一个CSV文件来写。 这里是代码:

def process_form_in_csv(self, order_id, order_date, order_fees): file_exists = os.path.isfile(self.SALES_SHEET) with open(self.SALES_SHEET, 'ab') as csvfile: fieldnames = ['Date','Order ID','Fee'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) if not file_exists: writer.writeheader() writer.writerow({'Date': order_date, 'Order ID': order_id, 'Seller Fee': order_fees, }) csvfile.close() 

这段代码可以工作,但是每当我重新运行程序时,第一行将被放在行的下方,并将3个单元格放在应该出现的位置。 如果我删除单元格(在Excel中),他们仍然存在。 我不知道发生了什么事。 附件是“空白”csv的样子。 CSV与删除的内容

我build议你看看: python打开内置函数:模式a,a +,w,w +和r +之间的区别?

  ``a'' Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar. 

  ``w'' Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.