I am trying to understand how to enumerate through a csv file and output the results to another csv file. For example, I have a csv file that has 2 columns, group_id and node_id. I want to use this csv file, loop through it and output the group_id, node_id and a formatted string that mentions the node_id. My code does exactly this when I use the print function however when I attempt to write to another csv file, only the last row is written.
Here is my code:
import csv
with open('input.csv', 'r') as f:
config_csv = csv.reader(f)
for row, column in enumerate(config_csv):
if row == 0:
continue
group_id = int(column[0])
sub_id = column[1]
node = f"The sub ID for this node is {sub_id}."
full_output=[group_id, sub_id, node]
print(full_output)
with open('output.csv', 'w') as file:
writer=csv.writer(file)
writer.writerow(full_output)
csv file (input.csv):
GROUP_ID,SUB_ID
675233,111
877531,222
455632,333
And my print output is:
[675233, 111, 'The sub ID for this node is 111.']
[877531, 222, 'The sub ID for this node is 222.']
[455632, 333, 'The sub ID for this node is 333.']
However my output file (output.csv) only shows the last line:
[455632, 333, 'The sub ID for this node is 333.']
What am I doing wrong? Why doesn't it output the same to the csv file that I see in the print function?