1

I have been working on a project for a little bit now. This project I am opening a CSV file for writing. Here is the code:

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()

This code works, but whenever I rerun the program the first line will be carried down the row and placed 3 cells over from where it should be. If I delete cells (in Excel) they remain. I don't know what is going on. Attached is an image of what the "blank" csv looks like. CSV with deleted content

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
Sleepy-Z
  • 83
  • 1
  • 12

1 Answers1

0

I suggest you to have a look at: python open built-in function: difference between modes a, a+, w, w+, and 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.

and

 ``w''   Truncate file to zero length or create text file for writing.
     The stream is positioned at the beginning of the file.
GSazheniuk
  • 1,340
  • 10
  • 16