1

I want to write a csv file with header in python and then append data to it by each run.But after each run the header as well as export_data is appended to the file. How can I prevent header to write each time?

ColA  ColB
item1 item2
item3 item4

Here is my code in python:

list1 = ['item1','item3']
list2 = ['item2','item4']
d = [list1,list2]
export_data = it.zip_longest(*d, fillvalue = '')
with open('D:/csv/csvfile.csv', 'a', newline='') as myfile:
    wr = csv.writer(myfile)
    wr.writerow(("colA", "colB",))
    wr.writerows(export_data)
h.m.gh
  • 87
  • 1
  • 10

0 Answers0