Skip to content Skip to sidebar Skip to footer

Add Column With A Header To A Tab-delimited Text File?

I realize that there is a way to add a column using 'awk'. But I'm not so familiar with this alternative, so I though I'd ask whether there's a way to add a column to a tab-delimit

Solution 1:

See answer here, a tab delimeted file is like CSV with tab as separator.

How to add a new column to a CSV file using Python?

Solution 2:

This is what I ended up doing:

withopen('ieca_first_col_fake_text.txt','r') asinput, \
   open('new_col_dict.txt', 'w') as output:
        dict_reader = csv.DictReader(input, delimiter = '\t')
        dict_reader.fieldnames.append('area')
        dict_reader.fieldnames.append('degrees')

        dict_writer = csv.DictWriter(output, 
                                     fieldnames=dict_reader.fieldnames, 
                                     delimiter='\t')
        for row in dict_reader:
            print row
            dict_writer.writeheader()
            dict_writer.writerow(row)

Post a Comment for "Add Column With A Header To A Tab-delimited Text File?"