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- ...
随机推荐
- solr 搜索引擎
http://www.cnblogs.com/wenxinghaha/p/4088790.html
- 没有找到MSVCR100.dll解决方法
转自:http://hi.baidu.com/fjdvd/blog/item/3679b201ec3d6b154afb515d.html MSVCR100.dll下载(游戏丢失msvcr100.dll ...
- MySQL DBA教程:Mysql性能优化之缓存参数优化
在平时被问及最多的问题就是关于 MySQL 数据库性能优化方面的问题,所以最近打算写一个MySQL数据库性能优化方面的系列文章,希望对初中级 MySQL DBA 以及其他对 MySQL 性能优化感 ...
- ANT的安装和配置(windows)
1.下载:到ANT官方网站http://ant.apache.org/下载最新版本,解压后即可.2.配置环境变量:我的电脑----属性-----高级----环境变量 如:ANT_HOME:C ...
- 【转】文件中有10G个整数,乱序排列,要求找出中位数
题目:在一个文件中有 10G 个整数,乱序排列,要求找出中位数.内存限制为 2G.只写出思路即可(内存限制为 2G的意思就是,可以使用2G的空间来运行程序,而不考虑这台机器上的其他软件的占用内存). ...
- 读写应用程序数据-NSUserDefault、对象归档(NSKeyedArchiver)、文件操作
ios中数据持久化存储方式一般有5种:NSUserDefault.对象归档(NSKeyedArchiver).文件操作.数据库存储(SQLite3).CoreData. 1.NSUserDefault ...
- My97DatePicker日期控件使用方法
My97DatePicker是一款网页版非常简单而且好用的日期控件,其实几年前就使用过了,这次再次用到,总结下: 首先去官网下载地址:http://www.my97.net/dp/down.asp 在 ...
- iOS 网络编程:NSURLSession
NSURLSession类和相关的类提供很多API来下载HTTP的内容.这些API提供多种delegate协议来支持验证和执行后台下载任务. 1 URL Session 设计概念 Session中的任 ...
- MySQL INSERT DELAYED
INSERT DELAYED 语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_nam ...
- 实现一个脚本语言Raven(一)
之前实现了Raven语言的0.1版,仅仅支持表达式处理与控制语句,由于不支持数组.函数.类,甚至都不是图灵完全的语言. 现在参考vczh的博客打算重新写一遍Raven语言.陈祖不愧是神啊,高中就写出支 ...