Python Forum
Problem with sum of values from .txt file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with sum of values from .txt file
#1
Hi Wink My task is to count difference between max and min value in each lines from .txt file then sum all these values. I've done the first part(the code below) but I don't know how best is it to sum all these values(I tried many ways).
I'll be grateful for help

filepath = 'rows.txt'  

with open(filepath) as fp:  
   line = [int(num) for num in fp.readline().split()]
   cnt = 1
   while line:
       x=max(line)-min(line)
    
       print("The difference between max-min value in line",cnt,"is :",x)
       cnt += 1
       #print(sum(x))
       #print(cnt)
       line = [int(num) for num in fp.readline().split()]
The result of this code:

Output:
The difference between max-min value in line 1 is : 2574 The difference between max-min value in line 2 is : 1088 The difference between max-min value in line 3 is : 1215 The difference between max-min value in line 4 is : 3852 The difference between max-min value in line 5 is : 1222 The difference between max-min value in line 6 is : 4324 The difference between max-min value in line 7 is : 1121 The difference between max-min value in line 8 is : 2465 The difference between max-min value in line 9 is : 2956 The difference between max-min value in line 10 is : 169 The difference between max-min value in line 11 is : 4644 The difference between max-min value in line 12 is : 992 The difference between max-min value in line 13 is : 1655 The difference between max-min value in line 14 is : 1422 The difference between max-min value in line 15 is : 1659 The difference between max-min value in line 16 is : 662
Reply


Messages In This Thread
Problem with sum of values from .txt file - by PathhK - Jan-07-2019, 07:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,681 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,331 Apr-01-2022, 11:33 PM
Last Post: Larz60+
  How to split file by same values from column from imported CSV file? Paqqno 5 2,802 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  Printing x values from an csv file hobbyist 7 3,998 Mar-10-2021, 02:00 PM
Last Post: hobbyist
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,057 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  problem returning values Naito 7 3,803 Jan-19-2019, 08:21 PM
Last Post: Naito
  Problem witrh else and elif values. anolibal 7 6,514 Aug-20-2018, 11:50 PM
Last Post: Skaperen
  trying to get the keystrokes values to file rwahdan 2 3,969 Jul-14-2017, 06:53 PM
Last Post: rwahdan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020