Append a Header for CSV file?

One way is to read all the data in, then overwrite the file with the header and write the data out again. This might not be practical with a large CSV file:

#!python3
import csv
with open('file.csv',newline='') as f:
    r = csv.reader(f)
    data = [line for line in r]
with open('file.csv','w',newline='') as f:
    w = csv.writer(f)
    w.writerow(['ColA','ColB'])
    w.writerows(data)

i think you should use pandas to read the csv file, insert the column headers/labels, and emit out the new csv file. assuming your csv file is comma-delimited. something like this should work:

   from pandas import read_csv

   df = read_csv('test.csv')
   df.columns = ['a', 'b']
   df.to_csv('test_2.csv')

I know the question was asked a long time back. But for others stumbling across this question, here's an alternative to Python.

If you have access to sed (you do if you are working on Linux or Mac; you can also download Ubuntu Bash on Windows 10 and sed will come with it), you can use this one-liner:

sed -i 1i"ColA,ColB" mycsvfile.csv

The -i will ensure that sed will edit in-place, which means sed will overwrite the file with the header at the top. This is risky.

If you want to create a new file instead, do this

sed 1i"ColA,ColB" mycsvfile.csv > newcsvfile.csv

Tags:

Python

Csv