I am new to Python and cannot deal with one of the points of my project, so I would be glad that you helped me :)
Suppose I have a * .txt file with only one column, which looks like this:
Column-1 row-1 0 row-2 25.00 row-3 27.14 row-4 29.29 row-5 31.43 row-6 33.57
* A column with rows has been added here to simplify the explanation.
I need to compute a second column that takes as input from column-1 and outputs the result of subtracting this row value from the previous one (if the value of row-1 (column-1) is 0, it should also be 0 in row-1 (column -2)). It should look like this:
- row-2 (column-2) = row-2 (column-1) - row-1 (column-1)
- row-3 (column-2) = row-3 (column-1) - row-2 (column-1), etc.
Let me show you what the output file will look like:
Column-1 Column-2 row-1 0 0 row-2 25.00 25.00 row-3 27.14 2.14 row-4 29.29 2.15 row-5 31.50 2.21 row-6 33.57 2.07
So far, I'm only here with my programming:
import sys with open('file.txt', "r") as f: sys.stdout = open('%s (calc).txt' % f.name, 'a') for line in f: column = line.strip().split() Column_1 = float(column[0]) column.extend([None])
I wonder what to do next. Maybe numpy is suitable for this task? I am not good at it (basically, I don’t know at all), should I find out?
In any case, I would be very grateful for your input.
source share