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学习平台
python学习_数据处理编程实例(二)的更多相关文章
- python学习_数据处理编程实例(一)
目的:用一个实例总结学习到的with语句,函数,列表推导,集合,排序,字符分割等内容 要求:分别以james,julie,mikey,sarah四个学生的名字建立文本文件,分别存储各自的成绩,时间格式 ...
- Python学习之==>面向对象编程(二)
一.类的特殊成员 我们在Python学习之==>面向对象编程(一)中已经介绍过了构造方法和析构方法,构造方法是在实例化时自动执行的方法,而析构方法是在实例被销毁的时候被执行,Python类成员中 ...
- Python学习_数据处理split方法
用open方法导入文件“sketch.txt”后,用split()方法进行分割: >>> import os >>> os.chdir('C:/Python33/H ...
- Python进阶:函数式编程实例(附代码)
Python进阶:函数式编程实例(附代码) 上篇文章"几个小例子告诉你, 一行Python代码能干哪些事 -- 知乎专栏"中用到了一些列表解析.生成器.map.filter.lam ...
- Python学习day39-并发编程(各种锁)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day38-并发编程(线程)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习:类和实例
Python学习:类和实例 本文作者: 玄魂工作室--热热的蚂蚁 类,在学习面向对象我们可以把类当成一种规范,这个思想就我个人的体会,感觉很重要,除了封装的功能外,类作为一种规范,我们自己可以定制的规 ...
- Python学习day40-并发编程(终)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day37-并发编程(3)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
随机推荐
- yaxim
Site: http://yaxim.org/yax.im/ Code: https://github.com/ge0rg/yaxim
- 自己动手写CPU之第七阶段(7)——乘累加指令的实现
将陆续上传本人写的新书<自己动手写CPU>.今天是第30篇.我尽量每周四篇 亚马逊的销售地址例如以下.欢迎大家围观呵! http://www.amazon.cn/dp/b00mqkrlg8 ...
- TRUNCATE TABLE 与 DELETE table 区别
语法 TRUNCATE TABLE name;参数 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行. TRUNCATE TABLE ...
- C#数据库读取数据后转换为INT32后计算的小技巧
这有什么难的,不管是什么数据库, 首先分别读出userinfo中usermoney的值 存入s1,card中extramoney的值s2 读出字段数据你应该会吧! 再用userinfo中字段userm ...
- C#泛型对类型参数的推断
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- http://venkatbaggu.com/file-upload-in-asp-net-mvc-using-dropzone-js-and-html5/
http://venkatbaggu.com/file-upload-in-asp-net-mvc-using-dropzone-js-and-html5/ http://www.cnblogs.co ...
- C# 的static与单例模式
C# 的static与单例模式 static是静态对象,在类被第一次使用,或者第一次被实例化时执行 /// <summary> /// 线程安全的单件模式 /// </summary ...
- c语言学习之基础知识点介绍(十三):枚举的介绍和使用
一.枚举的介绍 /* 枚举:限制的待选项. 语法: enum 枚举名{ 选项1, 选项2, 选项3, ........ 选项n }; 注意:枚举中,选项之间用 , 隔开,最后一个不用加 , :并且枚举 ...
- hadoop_集群安装_1
这篇文章中主要介绍的是,如何基于VM安装Linux,以及如何在安装好Linux之后,基于操作系统安装VMTools. 在安装之前,应该先规划好 每个node*的IP地址,以及 hostname: no ...
- thinkphp 行为扩展
网站程序在运行的过程每个过程都可以看做是一种行为,例如:运行应用,加载类,执行方法,加载模板,解析模板等,也就是说,我们在程序执行过程中每个 步骤都可以 定义一些点,我们可以在运行 程序的时候 检查 ...