python分析作业提交情况
Python  /  管理员 发布于 7年前   409
这次做一个比较贴近我实际的东西:python分析作业提交情况。
要求:
将服务器中交作业的学生(根据文件的名字进行提取)和统计成绩的表格中的学生的信息进行比对,输出所有没有交作业的同学的信息(学号和姓名),并输出所交的作业中命名格式有问题的文件名的信息(如1627406012_E03....)。
提示:
提示:
1、根据服务器文件可以拿到所有交了作业的同学的信息。
2、根据表格可以拿到所有上课学生的信息
3、对1和2中的信息进行比对,找出想要得到的信息
注意:提取服务器中学生交的作业的信息的时候应该考虑到文件格式不对的情况,所以提取信息的时候要做好相关的处理,以避免异常。
下面直接上程序(python3的版本):
#coding:utf-8 import os import xlrd """ 此函数用于获取dir文件夹中的文件的内容,dir中不能含有中文名 """ def getFilesInfo(dir): fileNum=dir[len(dir)-2:len(dir)] # 取得题目的编号 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] # 此函数用于读取xml表格文件中的内容 def readTableContent(fileName): date=xlrd.open_workbook(fileName) # sheet_name = date.sheet_names()[0] stuList=[] # 存放学号和姓名 try: # 获取你要处理的XLS的第一张表 sh = date.sheet_by_index(0) except: print("出现问题") for i in range(2,sh.nrows): id=sh.row_values(i)[1] name=sh.row_values(i)[2] student=(id,name); # 存放学生的学号和姓名的元组 stuList.append(student) return stuList address="D://我的文件/python作业批改/2016级老姜班级作业成绩 2016-10-25.xls" submitStuList=getFilesInfo("D:\E01") stuList=readTableContent(address) # 存放学生的信息的列表 notSubmitStudent=[] # 存放没有提交作业的学生的信息 for student in stuList: if student[0] not in submitStuList [0]: notSubmitStudent.append(student) print("===================没有交作业的人为=============") for student in notSubmitStudent: print(student[0],student[1]) print("===================格式错误的文件为=============") for error in submitStuList[1]: print(error)
对于上面的程序中,用到了一个读取表格的包xlrd,这个包需要自己进行下载,在pycharm中,直接进行如下步骤的下载:
1、首先进行如下操作:
2、然后点击“+”号(由于我是提前下载好了的,所以下面的图中有xlrd的包):
3、在输入框中输入包名并搜索
4、完成安装:
关于程序中使用到的os和xlrd的相关知识可以自行查阅相关的文档,这里不再进行详细说明。
下面是py2.7版本的程序:
#coding:utf-8 import os import xlrd import xlwt """ 此函数用于获取dir文件夹中的文件的内容,dir中不能含有中文名 """ def getFilesInfo(dir): fileNum=dir[len(dir)-2:len(dir)] # 取得题目的编号 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] # 此函数用于读取xml表格文件中的内容 def readTableContent(fileName): date=xlrd.open_workbook(fileName) # sheet_name = date.sheet_names()[0] stuList=[] # 存放学号和姓名 try: # 获取你要处理的XLS的第一张表 sh = date.sheet_by_index(0) except: print "出现问题" for i in range(2,sh.nrows): id=sh.row_values(i)[1].encode('utf-8') name=sh.row_values(i)[2] student=(id,name); # 存放学生的学号和姓名的元组 stuList.append(student) return stuList address=unicode("D://我的文件/python作业批改/2016级老姜班级作业成绩 2016-10-25.xls",'utf-8') # 对于中文名的路径要进行转换 submitStuList=getFilesInfo("D:\E01") stuList=readTableContent(address) # 存放学生的信息的列表 notSubmitStudent=[] # 存放没有提交作业的学生的信息 for student in stuList: if student[0] not in submitStuList [0]: notSubmitStudent.append(student) print "===============没有交作业的人为=============" for student in notSubmitStudent: print student[0],student[1] print "===============格式错误的文件为=============" for error in submitStuList[1]: print error
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号