Python Code For Obtaining The Mean Value From A Text File
Last Updated :
26 Feb, 2024
In Python programming, extracting data from files is a common task. One such task is to calculate the mean value of numerical data stored in a text file. This article will guide you through the process of writing a Python program to read data from a text file and compute the mean value.
Python Program to Calculate Mean Value from a Text File
Below are some of the ways by which we can calculate the mean value from a text file in Python:
data.txt
10
20
30
40
50
Calculate Mean Value from a Text File Using Basic Python Iteration
In this example, a file named “data.txt” is opened and read line by line. Each line, interpreted as an integer, is added to a running total, and the count of lines is incremented. Finally, the mean value is calculated by dividing the total by the count, and the result is printed as the mean value.
Python3
with open ( "data.txt" , "r" ) as file :
total = 0
count = 0
for line in file :
total + = int (line)
count + = 1
mean = total / count
print ( "Mean:" , mean)
|
Output:
Mean: 30.0
Calculate Mean Value from a Text File Using List Comprehension
In this example, the file “data.txt” is opened, and its contents are read into a list called lines
using readlines()
. Subsequently, the lines are converted to integers, stripping any leading or trailing whitespace, and stored in the data
list. The mean of the data is then calculated by dividing the sum of the values by the length of the list, and the result is printed as the mean value.
Python3
with open ( "data.txt" , "r" ) as file :
lines = file .readlines()
data = [ int (line.strip()) for line in lines]
mean = sum (data) / len (data)
print ( "Mean:" , mean)
|
Output:
Mean: 30.0
Calculate Mean Value from a Text File Using statistics Module
In this example, the file “data.txt” is opened and its contents are read line by line, converting each line to an integer and storing them in the data
list using a list comprehension. The mean of the data is then calculated using the statistics.mean
function from the Statistics module, and the result is printed as the mean value.
Python3
import statistics
with open ( "data.txt" , "r" ) as file :
data = [ int (line.strip()) for line in file ]
mean = statistics.mean(data)
print ( "Mean:" , mean)
|
Output:
Mean: 30.0
Calculate Mean Value from a Text File Using NumPy
In this example, the NumPy library is used to efficiently read numerical data from the “data.txt” file. The np.loadtxt
function is employed for this purpose. Subsequently, the mean value of the data is computed using np.mean
, and the result is printed as the mean value.
Python3
import numpy as np
data = np.loadtxt( "data.txt" )
mean_value = np.mean(data)
print ( "Mean:" , mean_value)
|
Output:
Mean: 30.0
Conclusion
In this article, we’ve discussed how to write a Python program to obtain the mean value from a text file. By utilizing file handling techniques and basic arithmetic operations, we can efficiently extract and analyze data stored in text files. This approach is applicable to various scenarios where data analysis or processing from files is required in Python programming.
Share your thoughts in the comments
Please Login to comment...