Python example application import Excel to complete simple calculation

Didn't go home during the summer vacation and stayed at school to study. At noon on the day when the final grades came out, did the counselor suddenly say something?
Insert picture description here
I instantly understood that something was done again. Sure enough, she sent me the grades of the whole grade, and asked me to calculate the weighted average grade (more than ten thousand pieces of data ahhhhhhhhhhhhhhhhhhhhhhhhh!!) . So this time I asked the big guy gyg to find out if I can use excel to calculate, but the big guy had a meal operation and calculated it directly with Java. This time I was really comfortable with the thief. I simply checked a few data and found that, The accuracy is not very accurate. Because the evaluation is needed, I gave up the data to find another way out; I originally planned to find a few people and directly calculate it manually, but this is really silly, I am sorry for my major, and I stay in school. I have learned a lot of python these days, so I want to try if I can learn from the big guys and write a similar little thing. I didn’t want to be successful by my rookie. (After talking so much, let’s talk about the problems I encountered in the process)

Description: Weighted average grade = Σ (course credits * course grades)/(Σ course credits)

First of all, I want to simply process the Excel spreadsheet and use the built-in function of Excel to calculate it (课程学分*课程成绩). But every time, the result of multiplying the data is inconsistent with expectations. I even doubt whether the built-in Excel function is spicy or not. I also suspect that I will pay attention to DY. Is a person who sends Excel skills (called him a little D) also a spicy chicken? All the methods can not be used, and then all kinds of doubts about life, all kinds of help. Finally, the gyg boss discovered the data type of credits and grades when calculating with Java 不是数字,不是数字,不是数字啊(really a bit crazy)

Insert picture description here

After converting the data into numbers, I discovered that the built-in functions of Excel are really powerful. The small D is really a good batch (broken tone). First, I added a blank line below each student’s grades, and then passed the small D technique , Basically it is one-click to find all the students Σ(课程学分*课程成绩)and all the students, Σ课程学分and then you only need to divide the two numbers. ! ! ! but! ! ! The written data is scattered under the scores of each student, and it cannot be calculated with one click through Excel.

Insert picture description here

Ever since, Python came on the scene, I Baidu a .xlsxthird-party library for manipulating files , I chose one named openpyxl, and then I wrote the python code:

First, I downloaded the openpyxllibrary
The idea is to write code: Due to Σ(课程学分*课程成绩)and Σ课程学分in front of an empty cell, so I wanted to be calculated by determining the first cell is empty, after some attempts, found that it seems not (think about it now, seems to forget to write rowback .valueThis led to the inability to judge (just guessing, not trying)), so I used the technique of small D, plus Baidu's method of filling multiple selected cells at the same time, which is a one-click to fill the empty cell with a number (expected). However, there is still one 文本类型addition. When I was judging, there was only one classmate’s grade, awsl. My unsatisfied mind was still stumbling on this one, and then I was thinking, should I still have one by one. Change the data type? Of course not. Since there are both numeric and text types, I will change the ifconditions. Whether you are text ( '1') or number ( 1) (as long as you are not a name), I will count. The following is the Python code, which is really very few Up.
from openpyxl import load_workbook  #调用

# 打开Excel文件
wb = load_workbook(filename='really.xlsx', data_only=True)
ws = wb.active

for row in ws.rows:
        if row[0].value in[ '1', 1]:
                print(row[2].value/row[1].value)

The code is written and ready to run the test, but it can’t find the prepared .xlsxfile. It’s really a bumpy road. Then I Baidu it again. The corresponding python script should be .xlsxplaced in the same directory as the file (I put it in After solving this, I started to test some of the data that I took out, and finally succeeded. Then, without saying anything, I just started to count the results of the whole grade. Effect picture?

Insert picture description here

I can use it when I stick it out, ah ha ha ha ha (sucking), and then simply organize, separate each class and it's done

Postscript: Because this can be directly pasted in the shell, I did not learn how to write the data back to the .xlsxfile, but this is already very fast (swish swish), can't hold back the joy hehehe, after this time, I I have a deeper understanding that knowledge is power, I have to come on! ! ! (To commemorate the first time programming to solve life problems)

Guess you like

Origin blog.csdn.net/weixin_43716048/article/details/96317667