如图 p1p2是书上的内容
p3这个程序是参考一位博主的
p4已经转换成了csv格式 内容自己编的
是附件有问题还是程序问题
下面是按二楼的改了之后的结果
def readFile(filename): #读取文件返回列表
file = open(filename,"r")
ls = list(line.strip().split(',') for line in file)
file.close()
return ls
def studentID(lsStudent,lsSchool,lsMajor):#生成学生学号
dicSchool = { x[0]:x[1] for x in lsSchool}
dicMajor = {x[0]: x[1] for x in lsMajor}
detail = []
for student in lsStudent:
id = '012'
id = id + student[5][2:] + dicSchool[student[2]] + dicMajor[student[3]] + student[4][-4:]
id = id + '{0:0>2}'.format([x[0] for x in lsStudent if student[4] == x[4]].index(student[0]) + 1)
detail.append([id] + student)
return detail
def info(stuName,lsStudent): #查找学生信息
info = [info for info in lsStudent if info[1] == stuName][0]
return info
def classmate(stuClass,lsStudent): #查找班级同学
classmate = [info for info in lsStudent if info[5] == stuClass]
return classmate
stuName = input()
stuClass = input()
studentList = 'studentList.csv'
schoolCode = 'schoolCode.csv'
MajorCode = 'MajorCode.csv'
lsStudent = readFile(studentList)
lsSchool = readFile(schoolCode)
lsMajor = readFile(MajorCode)
studentDetail = studentID(lsStudent, lsSchool, lsMajor)
print(*info(stuName,studentDetail))
lsclassmate = classmate(stuClass,studentDetail)
for classmate in lsclassmate:
print(*classmate)