Ishu Gupta
Ishu Gupta

Reputation: 1081

Append a line in already existing CSV

Below is the existing code for writing CSV , I would like to add a line right at the bottom of my existing csv

def writingcsv(req_id,Splunk_Alert_Id,ALERT_NAME,Error_Field,Message,Category):
     try:
      with open(log_csv, 'wb') as logcsv:
       row_count = sum(1 for row in logcsv)
       print row_count
       writer = csv.writer(logcsv)
       #writer.writerow((req_id,Splunk_Alert_Id,ALERT_NAME,Error_Field,Message,Category))
     finally:
       logcsv.close()

The above code is overwriting the existing data .

Upvotes: 0

Views: 49

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798446

The above code is overwriting the existing data .

Well yes, because that's what the "wb" mode does. Use "ab" to append instead.

Upvotes: 1

Related Questions