The code provided is a Python script that allows you to input and modify grades for students in an Excel file. Here is an overview of how the code works:

  1. The script begins by importing the necessary libraries, xlrd and xlwt, for reading and writing Excel files.

  2. You need to provide the name of the Excel file you want to work with. In this case, the file is named "理论力学21.xlsx".

  3. The variable "order" represents the range of rows in the Excel file that contain student information. It is a tuple with two elements, representing the start and end row numbers.

  4. The script opens the Excel file using the xlrd library and selects the first sheet.

  5. Three empty lists, "stu_name", "stu_order", and "stu_class", are created to store the student names, orders, and classes, respectively.

  6. A loop is used to iterate over the rows within the specified range. The student names, orders, and classes are extracted from the corresponding cells in the Excel sheet and appended to their respective lists.

  7. If a file named "tmp.tmp" exists and the user chooses to load the saved data, the script reads the contents of the file and populates the "tmp_order" and "tmp_grade" lists.

  8. The script then enters a while loop that repeatedly asks the user for input. The user can enter either a student name or order number. If the input ends with a number, it is assumed to be an order number and "2021" is appended to it.

  9. The script attempts to find the entered input in either the "stu_name" or "stu_order" lists. If found, the corresponding index is used to retrieve the student's name, order, and class from the respective lists.

  10. If the input is not found in either list, the script checks if it is a partial match to any names in the "stu_name" list. If a partial match is found, the script displays the matching student's index, name, and order number. The user is then prompted to enter the correct index.

  11. Once the student's information is retrieved, the script prompts the user to enter a grade for the student.

  12. If a corresponding entry is found in the "tmp_order" list, indicating that a grade has already been entered for the student, the script displays the existing grade and prompts the user to enter a new grade.

  13. If a corresponding entry is not found, the student's order number and the entered grade are appended to the "tmp_order" and "tmp_grade" lists, respectively.

  14. After the user finishes entering grades for all students, the script prompts the user whether to save the "tmp.tmp" file. If selected, the script writes the contents of the "tmp_order" and "tmp_grade" lists to the file.

  15. If the user chooses not to save the file, the script creates a new Excel file named "Test.xls" using the xlwt library. It then populates the first two columns of the first sheet with the order numbers and grades from the "tmp_order" and "tmp_grade" lists, respectively.

  16. Finally, the new Excel file is saved and the script ends.

Note: This code assumes that the Excel file follows a specific format, with student names in the third column, order numbers in the second column, and classes in the fifth column of the first sheet. You may need to modify the code if your Excel file has a different structure

import xlrdimport xlwtstu_file = 理论力学21xlsxorder = 1 87book = xlrdopen_workbookstu_filesheet = booksheets0stu_name = stu_order = stu_class = for i in rangeorder stu_nameappendsheetcell_valuei2 s

原文地址: https://www.cveoy.top/t/topic/iaGN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录