In the study period when the C language Teaching assistant, the teacher will decorate the homework, the students will be packed into a compressed file upload to the college FTP, my task is to download the students homework and statistics submitted number, sampling inspection operations. Student's compressed file by the number (9), name, date format named, so I would like to count the submission of the task can be solved in python, to alleviate my workload.
The basic idea of the program is:
1, the teacher gave me a save all student information (school number, name) of the Excel table, I first put all the student's school number into a dictionary key, value is initialized to 0 for the number of students submitted
2. Traverse all submitted files and remove the number and add 1 to the value of the corresponding key in the dictionary built in front
3, after the completion of the traversal, if the value stored in the dictionary is equal to 0, the corresponding student number of the classmate did not submit the assignment, the school number printed out
Of course, the above method will have errors (because some students do not format the file name, etc.), so I use the following methods to test:
After traversing the file, we know the total number of submissions, the number of repeated submissions, the subtraction is the total number of submissions, and did not submit the students add, whether the total number of students. If it is, on behalf of the number of students who did not submit the job is correct, otherwise, the number of students who did not submit the job is wrong, only manual statistics did not submit the work of the students. Fortunately, the number of students who do not submit jobs in most cases is correct, so the program still has practical application value.
The code is as follows:
Import Xlrdimport Osdata=xlrd.open_workbook (r ' k:\ work \ TA \ level 14 C job statistics. xls ') #打开excel, path can be changed table=data.sheets () [0] # Get the first sheet nrows=table.nrows #获取行数map ={} #保存学号及是否交作业for i in range (nrows): Cell=table.cell (i,1). Value Num=str (cell) [0:9] If Num.isdigit (): map[num]=0filenum=0for filename in Os.listdir (R ' k:\ work/Ta \20141224 '): #包含学生作业的文件夹, path can be changed Filen Um+=1 #文件夹中文件总数 studentid=str (filename) [0:9] #学生作业命名方式是前面9位为学号或者后面9位为学号 STUDENTID2=STR (filename) [ -13:-4] if studentid in Map:map[studentid]+=1 if studentID2 in Map:map[st udentid2]+=1notadmitnum=0# no number of submissions Keys=map.keys () print ("Not Delivered:") for key in Sorted (keys): #print (Key,map[key]) if Map[key] ==0:print (key) notadmitnum+=1admitmorenum=0# submit more than once print ("\ n submit more than once") for key in Sorted (keys): If Map[key ]>1:print (Key,map[key]) admitmorenum+= (map[key]-1) print ("\ n folder Total Files:%d"%filenum) print ("Number of repeat commits:%d"%admitmo renum) Print ("Total number of submissions:%d"% (filenum-admitmOrenum) Print ("Total number of people not paid:%d"%notadmitnum)
Statistics of students submitting homework