這次做一個(gè)比較貼近我實(shí)際的東西:python分析作業(yè)提交情況。
要求:
將服務(wù)器中交作業(yè)的學(xué)生(根據(jù)文件的名字進(jìn)行提取)和統(tǒng)計(jì)成績(jī)的表格中的學(xué)生的信息進(jìn)行比對(duì),輸出所有沒有交作業(yè)的同學(xué)的信息(學(xué)號(hào)和姓名),并輸出所交的作業(yè)中命名格式有問題的文件名的信息(如1627406012_E03....)。
提示:
提示:
1、根據(jù)服務(wù)器文件可以拿到所有交了作業(yè)的同學(xué)的信息。


2、根據(jù)表格可以拿到所有上課學(xué)生的信息

3、對(duì)1和2中的信息進(jìn)行比對(duì),找出想要得到的信息
注意:提取服務(wù)器中學(xué)生交的作業(yè)的信息的時(shí)候應(yīng)該考慮到文件格式不對(duì)的情況,所以提取信息的時(shí)候要做好相關(guān)的處理,以避免異常。
下面直接上程序(python3的版本):
#coding:utf-8 import os import xlrd   """ 此函數(shù)用于獲取dir文件夾中的文件的內(nèi)容,dir中不能含有中文名 """ def getFilesInfo(dir):   fileNum=dir[len(dir)-2:len(dir)]  # 取得題目的編號(hào)   trueList=[]   errorList=[]   t=os.walk(dir)   for item in t:     for name in item[2]:       if len(name)!=18:         errorList.append(name)       else:         if name[13:15]==fileNum:           trueList.append(name[0:10])         else:           errorList.append(name)   return [trueList,errorList]  # 此函數(shù)用于讀取xml表格文件中的內(nèi)容 def readTableContent(fileName):   date=xlrd.open_workbook(fileName)   # sheet_name = date.sheet_names()[0]   stuList=[]   # 存放學(xué)號(hào)和姓名   try: # 獲取你要處理的XLS的第一張表     sh = date.sheet_by_index(0)   except:     print("出現(xiàn)問題")   for i in range(2,sh.nrows):     id=sh.row_values(i)[1]     name=sh.row_values(i)[2]     student=(id,name); # 存放學(xué)生的學(xué)號(hào)和姓名的元組     stuList.append(student)   return stuList   address="D://我的文件/python作業(yè)批改/2016級(jí)老姜班級(jí)作業(yè)成績(jī) 2016-10-25.xls" submitStuList=getFilesInfo("D:/E01")  stuList=readTableContent(address)   # 存放學(xué)生的信息的列表  notSubmitStudent=[]   # 存放沒有提交作業(yè)的學(xué)生的信息 for student in stuList:   if student[0] not in submitStuList [0]:     notSubmitStudent.append(student) print("===================沒有交作業(yè)的人為=============") for student in notSubmitStudent:   print(student[0],student[1]) print("===================格式錯(cuò)誤的文件為=============") for error in submitStuList[1]:   print(error) 對(duì)于上面的程序中,用到了一個(gè)讀取表格的包xlrd,這個(gè)包需要自己進(jìn)行下載,在pycharm中,直接進(jìn)行如下步驟的下載:
新聞熱點(diǎn)
疑難解答
圖片精選