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
#2
(Jan-07-2019, 07:11 PM)PathhK Wrote: I tried many ways
So, what did you tried? Post your best attempt
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
line is a list of the ints in the line. So if you create a new variable, maybe total outside the while loop, you can do: total += sum(line).

Also, why are you using a while loop? Iterating over the file directly is cleaner:
with open("thefile") as fp:
    total = 0
    for cnt, line in enumerate(fp):
        nums = [int(num) for num in line.split()]
        total += sum(nums)
        diff = max(nums) - min(nums)
        print(f"The difference between max-min value in line {cnt} is: {diff}")
    print(f"The total is : {total}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,649 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Overwrite values in XML file with values from another XML file Paqqno 5 3,296 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,770 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  Printing x values from an csv file hobbyist 7 3,957 Mar-10-2021, 02:00 PM
Last Post: hobbyist
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,033 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  problem returning values Naito 7 3,780 Jan-19-2019, 08:21 PM
Last Post: Naito
  Problem witrh else and elif values. anolibal 7 6,481 Aug-20-2018, 11:50 PM
Last Post: Skaperen
  trying to get the keystrokes values to file rwahdan 2 3,959 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