Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add headers to a file

I have a file containing data like below:

88_NPDJ    565    789   3434   54454
98HGJDN    945    453   3453   23423
...
...
...

whats the best way to add headers to the file? After data has been entered into the file. The data is tab delimited.

like image 808
Harpal Avatar asked Dec 29 '22 11:12

Harpal


1 Answers

Best way to get the effect of altering a file in place is with fileinput:

import fileinput

headers = 'a b c d e'.split()
for line in fileinput.input(['thefile.blah'], inplace=True):
    if fileinput.isfirstline():
        print '\t'.join(headers)
    print line,
like image 172
Alex Martelli Avatar answered Jan 08 '23 22:01

Alex Martelli