Python Forum
Statistics from text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statistics from text file
#1
import statistics
import math


  
def load_stats(filename):
    #load file containing integers
    #integers are separated by a space

    data_list = []
    with open('stats.txt', 'r') as f:
        for line in f:
            (x) = line.strip().split(',')
            data_list+=[x]
            data_list.sort()
    return (data_list)


def main():
    done = False
    while not done:
        (data_list) = load_stats('stats.txt')
        print ('Choose an option:')
        print ('1. Load data')
        print ('2. Display computed statistics')
        print ('3. Save computed statistics')
        print ('4. Exit')
        user = input(str('Choice:'))
        if user == '1':
            file_name = input('Enter the name of the file:')
            print ('Data read successfully')
        elif user == '2':
            print ('Below are the computed values:')
            print ('Min =', min(data_list))
            print ('Max =', max(data_list))
            
           
        
        elif user == '3':
            new_file = input ('Enter the name of the file:')
            print ('Result saved successful.')
        elif user == '4':
            print ('Goodbye!')
            done = True
main()
I am having issues with it just printing out the min and max value. It gives this to me instead.
Choose an option:
1. Load data
2. Display computed statistics
3. Save computed statistics
4. Exit
Choice:2
Below are the computed values:
Min = ['87', ' 63', ' 100', ' 94', ' 56', ' 77', ' 60', ' 72', ' 99', ' 83']
Max = ['87', ' 63', ' 100', ' 94', ' 56', ' 77', ' 60', ' 72', ' 99', ' 83']
Choose an option:
I also have to add in mean and median but cannot seem to find the right modules that I need to call so they work.
I have tried both import statistics and import math.
I am on python 3.

Attached Files

.txt   stats.txt (Size: 39 bytes / Downloads: 529)
Reply
#2
they are few ways...
if me i'll convert each item in line.strip().split(',') to integer 1st, then add into data_list. pretty sure min/median formula are short/simple to code, u dont need to import any modules for that, its up to u anyway

data_list = []
with open('stats.txt', 'r') as f:
    for line in f:
        for number in line.strip().split(','):     #line  changed
            data_list+=[int(number)]               #line  changed
    data_list.sort()
return (data_list)
swallow osama bin laden
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Collecting Average User Statistics? 1st semester programmer Jagsrs28 3 2,288 Mar-05-2020, 08:05 PM
Last Post: Jagsrs28
  Convert text from an image to a text file Evil_Patrick 5 4,262 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,365 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,197 Jul-21-2018, 07:32 PM
Last Post: EvanCahill

Forum Jump:

User Panel Messages

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