1

I want to add a new column in an existing file and would like to write the output to another file. I open the file as follows and add my required statements. How do I write my output to the file by adding a new column at the end( With column name/Header). The separation is tab.

with open(newfile, 'w') as outfile:
    with open(oldfile, 'r', encoding='utf-8') as infile:
        statements:

sample of input:

Col1 Col2 Col3 Col4

Val1 Val1 Val1 Val1
Val2 Val2 Val2 Val2
Val3 Val3 Val3 Val3
Val4 Val4 Val4 Val4

sample of output:

Col1 Col2 Col3 Col4 Col5(Newly added)

Val1 Val1 Val1 Val1 Val1
Val2 Val2 Val2 Val2 Val2
Val3 Val3 Val3 Val3 Val3
Val4 Val4 Val4 Val4 Val4

Thanks in advance.

1
  • The question is not clear. The output sample you have shown that is for the existing file, right? Commented Oct 17, 2015 at 11:34

2 Answers 2

1
import csv

with open('data','r') as f_in:
    with open('data_out', 'w') as f_out:
        writer = csv.writer(f_out, delimiter=' ', lineterminator='\n')
        reader = csv.reader(f_in, delimiter=' ')

        result = []
        # read headers
        row = next(reader)
        # add new header to list of headers
        row.append('Col5')
        result.append(row)

        for row in reader:
            # add new column values
            row.append(row[0])
            result.append(row)

        writer.writerows(result)

data_out 

Col1 Col2 Col3 Col4 Col5
Val1 Val1 Val1 Val1 Val1
Val2 Val2 Val2 Val2 Val2
Val3 Val3 Val3 Val3 Val3
Val4 Val4 Val4 Val4 Val4
Sign up to request clarification or add additional context in comments.

Comments

0

You can write the following code assuming you know beforehand the name of the new column, if that's not the case you can compute it on the first_line condition inside the for loop.

Also, we are taking the last value from the values row (and setting it as the last value on every row), if you need another behaviour, just change the else part inside the for loop.

new_column_name = 'Col5'
with open(newfile, 'w') as outfile:
    with open(oldfile, 'r', encoding='utf-8') as infile:
        for line in infile:
            if first_line:
                outfile.write('{} {}\n'.format(line, new_column_name))
                first_line = False
            else:
                values = line.split()
                if values:
                    values.append(values[-1])
                outfile.write(' '.join(values) + '\n')

Hope it helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.