Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How python reads files and calculates averages

Shulou Source: shulou.com Published: 2022-05-31 11:15:52 10月03日 Update

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.

Tags: Files averages content knowledge projects objects totals quantities methods industries built different practical functional practical practical articles more cases steps Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Shulou Technology Apple OPPO Reno