0

I am trying to calculate the sum of produced power in a CSV file.

  • This schould be the last line of the file, second value - first line of the file, second value

or

  • Sum of every value at the end of the line..

    07:15;4719.852;0.036

    07:20;4719.857;0.060

    07:25;4719.863;0.072

    .

    .

    08:30;4720.399;0.924

I've noticed that is seperatet by ";" in stead of a ","

Since I cant figure option 1 out I've gone in calculating the sum of every line.

First is change the ";" in ","

cat  /home/LoadLive/day.csv  | tr ";" "," >  /home/LoadLive/day1.csv

Then I use the python script

import csv
with open('/home/LoadLive/day1.csv', 'r') as f:
  total = 0
  for row in csv.reader(f):
    total += float(row[2])
  print('The total is {}'.format(total))

And the result is "The total is 0.9600000000000001"

Where does the 000000001 comes from? and can I do this in a better way?

Tanks

AcK
  • 2,063
  • 2
  • 20
  • 27
Geertz
  • 1

1 Answers1

-1

You could try to use pandas to load your data and to compute the sum.

Using this, you'll not need to change manually your separator from ";" to ",", you can simply use the sep parameter from pandas.read_csv()

Check this for the csv : https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html Check this for the sum : https://www.statology.org/sum-columns-pandas/