Python - computing the second column from the first in a file

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.

+5
source share
3 answers

I believe that this would do what you asked:

 INPUT = 'file.txt' OUTPUT = 'calc.txt' def main(): with open(INPUT, 'r') as reader, open(OUTPUT, 'a') as writer: last_value = 0 for line in reader: column_1, *remaining_columns = map(float, line.split()) column_2 = column_1 - last_value last_value = column_1 print(column_1, column_2, sep='\t', file=writer) if __name__ == '__main__': main() 
+5
source

One approach might be this: let's say you have two lists:

 a = [1,2,3,0] b = [0,1,2,3] 

You can then subtract one list from another using the following step:

 import operator map(operator.sub, a, b) 

To do this, you will need to read in your file as an array (using array.append(value) to get all the data.

Then make a copy of your data and copy them to one (the length of the list should be the same). How you handle the beginning and end of the array depends on how important these values ​​are to you (maybe you can afford to lose one value).

+2
source

Here is a solution using lists and zip:

 #!/usr/bin/env python3 with open('file.txt', "r") as f: # read column one into a list column_1 = [float(line.strip()) for line in f] # compute differences of neighbouring values column_2 = [now - last for now, last in zip(column_1, [0.0]+column_1[:-1])] with open("result.txt", "w") as outfile: for c1, c2 in zip(column_1, column_2): print("{:.2f}\t{:.2f}".format(c1, c2), file=outfile) 

What happens here, we first create a list of all the entries from the input file. Using zip, we can create tuples of two (or more) iterations. We need to create a second list with values ​​shifted by 1 and adding 0.0 in front to act as a control unit for the first record.

Now we can combine the two lists together and calculate the differences between pairs of values ​​using a second list comprehension.

Learning numpy is always a good idea, but I think it will be redundant for this task.

+2
source

All Articles