在上一节python学习_数据处理编程实例(二)的基础上数据发生了变化,文件中除了学生的成绩外,新增了学生姓名和出生年月的信息,因此将要成变成:分别根据姓名输出每个学生的无重复的前三个最好成绩和出生年月
数据准备:分别建立四个文本文件
james2.txt James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
julie2.txt Julie Jones,2002-8-17,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
mikey2.txt Mikey McManus,2002-2-24,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
sarah2.txt Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
在上一节基础上,修改部分代码,将新要求实现如下:
1 import os 2 print(os.getcwd()) 3 os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter6') #将工作空间修改为文件所在的目录 4 5 #定义函数get_filedata从文件中取值 6 def get_filedata(filename): 7 try: 8 with open(filename) as f: #with语句打开和自动关闭文件 9 data=f.readline() #从文件中逐行读取字符 10 data_list=data.strip().split(',') #将字符间的空格清除后,用逗号分隔字符 11 return({ 12 "name" : data_list.pop(0), 13 "date_of_birth" : data_list.pop(0), 14 "times" : str(sorted(set([modify_time_format(s) for s in data_list]))[0:3]) 15 }) #使用字典将关联的姓名,出生年月,时间键和值进行存储并返回 16 except IOError as ioerr: 17 print ('File Error' + str(ioerr)) #异常处理,打印错误 18 return (None) 19 20 #定义函数modify_time_format将所有文件中的时分表达方式统一为“分.秒” 21 def modify_time_format(time_string): 22 if "-" in time_string: 23 splitter="-" 24 elif ":" in time_string: 25 splitter=":" 26 else: 27 splitter="." 28 (mins, secs)=time_string.split(splitter) #用分隔符splitter分隔字符后分别存入mins和secs 29 return (mins+ '.' +secs) 30 31 #定义函数get_prev_three返回文件中排名前三的不重复的时间成绩 32 def get_prev_three(filename): 33 new_list=[modify_time_format(each_t) for each_t in get_filedata(filename)] #采用列表推导将统一时分表达方式后的记录生成新的列表 34 delete_repetition=set(new_list) #采用集合set函数删除新列表中重复项,并生成新的集合 35 in_order=sorted(delete_repetition) #采用复制排序sorted函数对无重复性的新集合进行排序 36 return (in_order[0:3]) 37 38 #输出james的排名前三的不重复成绩和出生年月 39 james = get_filedata('james2.txt') 40 print (james["name"]+"'s fastest times are: " + james["times"]) 41 print (james["name"] + "'s birthday is: " + james["date_of_birth"]) 42 43 #输出julie的排名前三的不重复成绩和出生年月 44 julie = get_filedata('julie2.txt') 45 print (julie["name"]+"'s fastest times are: " + julie["times"]) 46 print (julie["name"] + "'s birthday is: " + julie["date_of_birth"]) 47 48 #输出mikey的排名前三的不重复成绩和出生年月 49 mikey = get_filedata('mikey2.txt') 50 print (mikey["name"]+"'s fastest times are: " + mikey["times"]) 51 print (mikey["name"] + "'s birthday is: " + mikey["date_of_birth"]) 52 53 #输出sarah的排名前三的不重复成绩和出生年月 54 sarah = get_filedata('sarah2.txt') 55 print (sarah["name"]+"'s fastest times are: " + sarah["times"]) 56 print (sarah["name"] + "'s birthday is: " + sarah["date_of_birth"])
通过建立继承内置list的类AthleteList,将方法定义在类中实现相同功能:
1 import os 2 print(os.getcwd()) 3 os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter6') #将工作空间修改为文件所在的目录 4 5 #定义类AthleteList继承python内置的list 6 class AthleteList(list): 7 def __init__(self, name, dob=None, times=[]): 8 list.__init__([]) 9 self.name=name 10 self.dob=dob 11 self.extend(times) 12 def get_prev_three(self): 13 return (sorted(set([modify_time_format(t) for t in self]))[0:3]) 14 15 def get_filedata(filename): 16 try: 17 with open(filename) as f: #with语句打开和自动关闭文件 18 data=f.readline() #从文件中逐行读取字符 19 data_list=data.strip().split(',') #将字符间的空格清除后,用逗号分隔字符 20 return( 21 AthleteList(data_list.pop(0), data_list.pop(0), data_list) 22 ) #使用字典将关联的姓名,出生年月,时间键和值进行存储并返回 23 except IOError as ioerr: 24 print ('File Error' + str(ioerr)) #异常处理,打印错误 25 return (None) 26 27 def modify_time_format(time_string): 28 if "-" in time_string: 29 splitter="-" 30 elif ":" in time_string: 31 splitter=":" 32 else: 33 splitter="." 34 (mins, secs)=time_string.split(splitter) #用分隔符splitter分隔字符后分别存入mins和secs 35 return (mins+ '.' +secs) 36 37 james = get_filedata('james2.txt') 38 print (james.name+"'s fastest times are: " + str(james.get_prev_three())) 39 40 julie = get_filedata('julie2.txt') 41 print (julie.name+"'s fastest times are: " + str(julie.get_prev_three())) 42 43 mikey = get_filedata('mikey2.txt') 44 print (mikey.name+"'s fastest times are: " + str(mikey.get_prev_three())) 45 46 sarah = get_filedata('sarah2.txt') 47 print (sarah.name+"'s fastest times are: " + str(sarah.get_prev_three()))
参考资料:HeadFirstPython系列书籍chapter 6
强烈推荐新手学习HeadFirstPython书籍,需要pdf文档的私信联系,另外推荐Codecademy学习平台