How python reads files and calculates averages
This article mainly introduces the relevant knowledge of "how python reads the file and calculates the average value". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to read the file and calculate the average value of python" can help you solve the problem.
The specific steps are as follows:
1. First, open python and create a new python project
After the 2.python project is created, use the open () function in the project to open a file and create a file object
File = open ('test.txt', 'r')
3. After the file object is created, use the read () method to get the contents of the file
Res = file.read ()
4. After getting the contents of the file, iterate through the for loop to get the total number and number of files
For line in infile:
Amount = float (line)
Total + = amount
Length = length + 1
5. Finally, after the total number and number of files are obtained, the average value can be obtained by dividing the two numbers.
Average = total / length
Print (average)
That's all for "how python reads files and calculates averages". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.