I am trying to read first and third columns from a text file and add them together.
Following code works perfectly and gives me the result I need but trying to find out if there is a better more pythonic way to write this?
with open('random.txt', 'r') as fn:
next(fn)
numbers = fn.readlines()
first_col = [int(x.split(',')[0]) for x in numbers]
third_col = [int(y.split(',')[2]) for y in numbers]
result = [v + z for v, z in zip(first_col, third_col)]
print(result)
The random file is literally a random file.
col1,col2,col3
44,65,78
55,87,98
12,32,62
Result:
[122, 153, 74]