簡體   English   中英

寫入CSV python時銷毀空白單元格

[英]Destroy blank cells when writing to 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內容已刪除

我建議您看一下: 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM