Python Forum

Full Version: Add\insert header row existing csv files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

I am opening existing csv files, and Want to insert records at first row.

if I use inplace=True , file get overwrites
if I use inplace=False , nothing get changed.

import fileinput

for line in fileinput.input(files=["my-file.csv"], inplace=True):
    if fileinput.isfirstline():
        print('first,second,third,fourth')
    print(line, end="")