Yes can do it like this.
Here an other version you can look at using
Also put in header line just as a example.
See that with string formatting(f-string) it look better no need for
Here an other version you can look at using
f-string
.Also put in header line just as a example.
See that with string formatting(f-string) it look better no need for
str()
or +
.with open('col.txt') as f,open('total.txt', 'w') as f_out: header = next(f) c1, c2 = header.strip().split(',') f_out.write(f'{c1} {c2} Sum\n') for line in f: line = line.strip() num1, num2 = line.split(',') num1, num2 = int(num1), int(num2) num3 = num1 + num2 f_out.write(f'{num1} + {num2} ${num3}\n')
Output:colA colB Sum
324 + 234 $558
346 + 341 $687
147 + 346 $493
234 + 567 $801
368 + 405 $773
344 + 643 $987
235 + 235 $470
236 + 567 $803