python学习_数据处理编程实例(二)

上一节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

在上一节基础上,修改部分代码,将新要求实现如下:

 import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter6') #将工作空间修改为文件所在的目录 #定义函数get_filedata从文件中取值
def get_filedata(filename):
try:
with open(filename) as f: #with语句打开和自动关闭文件
data=f.readline() #从文件中逐行读取字符
data_list=data.strip().split(',') #将字符间的空格清除后,用逗号分隔字符
return({
"name" : data_list.pop(0),
"date_of_birth" : data_list.pop(0),
"times" : str(sorted(set([modify_time_format(s) for s in data_list]))[0:3])
}) #使用字典将关联的姓名,出生年月,时间键和值进行存储并返回
except IOError as ioerr:
print ('File Error' + str(ioerr)) #异常处理,打印错误
return (None) #定义函数modify_time_format将所有文件中的时分表达方式统一为“分.秒”
def modify_time_format(time_string):
if "-" in time_string:
splitter="-"
elif ":" in time_string:
splitter=":"
else:
splitter="."
(mins, secs)=time_string.split(splitter) #用分隔符splitter分隔字符后分别存入mins和secs
return (mins+ '.' +secs) #定义函数get_prev_three返回文件中排名前三的不重复的时间成绩
def get_prev_three(filename):
new_list=[modify_time_format(each_t) for each_t in get_filedata(filename)] #采用列表推导将统一时分表达方式后的记录生成新的列表
delete_repetition=set(new_list) #采用集合set函数删除新列表中重复项,并生成新的集合
in_order=sorted(delete_repetition) #采用复制排序sorted函数对无重复性的新集合进行排序
return (in_order[0:3]) #输出james的排名前三的不重复成绩和出生年月
james = get_filedata('james2.txt')
print (james["name"]+"'s fastest times are: " + james["times"])
print (james["name"] + "'s birthday is: " + james["date_of_birth"]) #输出julie的排名前三的不重复成绩和出生年月
julie = get_filedata('julie2.txt')
print (julie["name"]+"'s fastest times are: " + julie["times"])
print (julie["name"] + "'s birthday is: " + julie["date_of_birth"]) #输出mikey的排名前三的不重复成绩和出生年月
mikey = get_filedata('mikey2.txt')
print (mikey["name"]+"'s fastest times are: " + mikey["times"])
print (mikey["name"] + "'s birthday is: " + mikey["date_of_birth"]) #输出sarah的排名前三的不重复成绩和出生年月
sarah = get_filedata('sarah2.txt')
print (sarah["name"]+"'s fastest times are: " + sarah["times"])
print (sarah["name"] + "'s birthday is: " + sarah["date_of_birth"])

通过建立继承内置list的类AthleteList,将方法定义在类中实现相同功能:

 import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter6') #将工作空间修改为文件所在的目录 #定义类AthleteList继承python内置的list
class AthleteList(list):
def __init__(self, name, dob=None, times=[]):
list.__init__([])
self.name=name
self.dob=dob
self.extend(times)
def get_prev_three(self):
return (sorted(set([modify_time_format(t) for t in self]))[0:3]) def get_filedata(filename):
try:
with open(filename) as f: #with语句打开和自动关闭文件
data=f.readline() #从文件中逐行读取字符
data_list=data.strip().split(',') #将字符间的空格清除后,用逗号分隔字符
return(
AthleteList(data_list.pop(0), data_list.pop(0), data_list)
) #使用字典将关联的姓名,出生年月,时间键和值进行存储并返回
except IOError as ioerr:
print ('File Error' + str(ioerr)) #异常处理,打印错误
return (None) def modify_time_format(time_string):
if "-" in time_string:
splitter="-"
elif ":" in time_string:
splitter=":"
else:
splitter="."
(mins, secs)=time_string.split(splitter) #用分隔符splitter分隔字符后分别存入mins和secs
return (mins+ '.' +secs) james = get_filedata('james2.txt')
print (james.name+"'s fastest times are: " + str(james.get_prev_three())) julie = get_filedata('julie2.txt')
print (julie.name+"'s fastest times are: " + str(julie.get_prev_three())) mikey = get_filedata('mikey2.txt')
print (mikey.name+"'s fastest times are: " + str(mikey.get_prev_three())) sarah = get_filedata('sarah2.txt')
print (sarah.name+"'s fastest times are: " + str(sarah.get_prev_three()))

参考资料:HeadFirstPython系列书籍chapter 6

强烈推荐新手学习HeadFirstPython书籍,需要pdf文档的私信联系,另外推荐Codecademy学习平台

上一篇:集合(Collection,set,list,map)


下一篇:二分查找算法(Python版)