Reputation: 19
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.
Upvotes: 1
Views: 2451
Reputation: 5658
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
Upvotes: 1
Reputation: 3043
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,
Upvotes: 0