I have a txt file containing on each line 2 values separated by a space:
x1 y1
x2 y2
x3 y3
...
xn yn
I want to get another file containing:
x1 y1
x2 y1+y2
x3 y1+y2+y3
...
xn y1+y2+y3+...+yn
What is the quickest (I mean easiest) way to do this in python ?
This will get you started.
Given
data.txt:and this code snippet:
will give you (using
float()above):On the other hand, if you wanted the lines with the
+:will give you (using
int()this time):I suspect the above code, esp the 2nd one, could probably be streamlined/optimized more, but should be sufficient to get you started.
You still will have adjust the conversion from string to appropriate type (
floatorint) and also create the output file and write to it in the format you prefer. These are details you can best decide.