Search code examples
pythongoogle-colaboratorytext-files

Using a data text file in Colab to calculate mean


I am new to python so any assistance is greatly appreciated. I am writing a calculator to generate mean from a text file in google colab.

I am able to calculate the mean by hardcoding numbers, and I am also able to access a text file in a separate piece of code. My issue is passing the data in the txt file to the mean function.

'''
####This writes data in from txt file
filename = '/content/drive/MyDrive/ColabNotebooks/data.txt' 

with open(filename) as file_object:
    lines = file_object.readlines()
    
for line in lines:
   print(line)
'''


'''
 #Calculating the Mean
def calculate_mean(numbers):
  s=sum(numbers)
  N=len(numbers)
# Calculate the mean
  mean=s/N

  return mean

if __name__=='__main__':
  
  donations =[100,100,200,1000]
  mean=calculate_mean(donations)
  N= len(donations)
  print('Mean donation over the last {0} days is {1}'.format(N,mean))
'''

Question how do i modify 'donations' to obtain the list from the text file data.txt instead of the hardcoded values

Here is a screenshot of values in the txt file. This is a learning exercise for me. data

Again many thanks!


Solution

  • Alright so you are a little out of sorts here. You need to parse the incoming data based on the format. If your delimiter is a space as in your example, the following code should work.

         #Calculating the Mean
        def calculate_mean(numbers):
          s=sum(numbers)
          N=len(numbers)
        # Calculate the mean
          mean=s/N    
          return mean
        
        if __name__=='__main__':
             ####This writes data in from txt file
             filename = '/content/drive/MyDrive/ColabNotebooks/data.txt' 
        
             with open(filename) as file_object:
                  lines = file_object.readlines()
            
             for line in lines:
                  print(line)
                  donations_as_strings=line.split(" ") # " " is you delimiter
                  donations = map(int,donations_as_strings) # If using python 2.X
                  donations = list(map(int,donations_as_strings)) # if using python 3.X
                  # or just do this
                  donations = [int(i) for i in donations_as_strings]
                  mean=calculate_mean(donations)
                  print(donations) # Should be [#,#,#]
                  N= len(donations)
                  print('Mean donation over the last {0} days is {1}'.format(N,mean))