'How to append a new column in existing CSV file?

I have a csv file with the data :

TableName  , Rows , Columns , StartTime
DimAccount ,  40  ,   12    ,  18.7

I need to append a new column to this CSV, such that the output is :

TableName  , Rows , Columns , StartTime , EndTime
DimAccount ,  40  ,   12    ,  18.7     ,  20

I tried using :

fieldnames = ['EndTime']
with open(data.csv', 'a') as csvfile:
     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
     writer.writeheader()
     writer.writerow({'EndTime': 20})

but it gives output as :

TableName  , Rows , Columns , StartTime
DimAccount ,  40  ,   12    ,  18.7
Endtime
20


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source