Nanny level explains Python data processing, you can definitely understand

Name: Ah Yue's Little Dongdong

Learning: Python, C/C++

Home page link: A Yue's Xiaodongdong's blog_CSDN blog-python&&c++ advanced knowledge, essential for Chinese New Year, blogger in the field of C/C++ knowledge explanation

Table of contents

1. File reading

2. Data processing

3. Processing result output

In general


In order for us to run the program, we need to install Python, just click to download and install the latest Python version from the official website. The following points should be paid attention to during the installation process:

  1. During the installation process, check "Add python.exe to Path" to add Python environment variables.
  2. Windows users need to enter "python" in the command line, and Mac users need to enter "python3" in the terminal to check whether Python is installed successfully.

Next, we can start writing programs. This article will demonstrate a simple program that reads data from a file and performs simple processing, and finally outputs the processing result.

Before writing programs, we need to know some basic Python syntax. Python is an object-oriented programming language whose core idea is simplicity, elegance and clarity. The code of Python is very easy to read and understand, and the syntax is also very simple, which is suitable for introductory learning.

The implementation of the program requires the following parts:

  1. File reading and writing: Use Python's built-in open() function to open files, read or write data.
  2. Data manipulation: Manipulate data using Python's built-in data structures and functions.
  3. Output result: Use Python's print() function to output the result.

Now let's implement this program in detail.

First, we need to prepare a data file and save it on the local disk in .txt format. This time the program will read the data from the file for processing, and then output the result.

Here we simulate a data file, the file name is data.txt, and the file content is as follows:

A 1
B 2
C 3
D 4
E 5

Next, we will write a Python program to read the file, process the data in the file, and then output the processing result.

1. File reading

We read data from the file and store the data in the list data. We can use the with statement to open the file, which ensures that the file is automatically closed after use. code show as below:

with open('data.txt', 'r') as file:
    data = file.readlines()

The above code first opens the file data.txt and reads all the lines into the list data. We can use the readlines() function to read all the lines in the file. After reading, we can use the print function to print the data to check whether the data is read correctly.

print(data)

Output result:

['A 1\n', 'B 2\n', 'C 3\n', 'D 4\n', 'E 5\n']

We can see that the read data has been saved in the list data.

2. Data processing

Now, we need to process the read data and store the processed results. We split the data into two parts and store them in two different lists, one for letters and one for numbers. code show as below:

letters = []
numbers = []

for line in data:
    line = line.strip()
    letter, number = line.split()
    letters.append(letter)
    numbers.append(int(number))

The above code first defines two empty lists letters and numbers, and then iterates through each row in the data list. The strip() function is used to remove newline characters and spaces in each line, and the split() function is used to divide each line into two parts according to spaces, one is a letter and the other is a number. These two parts are then stored in the letters and numbers lists respectively. When storing a number, it needs to be converted to an integer type.

3. Processing result output

Now that we have processed the data and stored them in the lists letters and numbers, we need to further process them, calculate their average, and output the result to the command line window. code show as below:

average = sum(numbers) / len(numbers)
print('The average number is {:.2f}'.format(average))

The above code first uses the sum function to calculate the sum of all elements in the numbers list, and then divides by the number of elements in the list to get the average. When outputting, we use the format function to format the data, keeping two digits after the decimal point.

The complete program code is as follows:

with open('data.txt', 'r') as file:
    data = file.readlines()

letters = []
numbers = []

for line in data:
    line = line.strip()
    letter, number = line.split()
    letters.append(letter)
    numbers.append(int(number))

average = sum(numbers) / len(numbers)
print('The average number is {:.2f}'.format(average))

Running the program, we can get the output:

The average number is 3.00

This time, a simple program is implemented, which reads data from a file and processes it, and finally outputs the processing result. Through this example, we have learned the basic syntax and functions of Python, and understood the basic concepts of file reading, data processing and output, and have a deeper understanding of Python programming.

In general

, Python is a very good programming language, it is easy to learn, powerful, suitable for project development of all sizes. Python has a wide range of applications in data analysis, artificial intelligence, natural language processing, web development, crawlers and other fields, and is one of the languages ​​worthy of our in-depth study and mastery.

Guess you like

Origin blog.csdn.net/m0_64122244/article/details/132332510