[Head First Python]5. 推导数据:处理数据
2014-06-18 15:33
363 查看
读取4个文件内容,格式化数据,升序,显示每个文件前3个数据
julie.txt
james.txt
sarah.txt
mikey.txt
chapter5.py
julie.txt
2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
james.txt
2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
sarah.txt
2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
mikey.txt
2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
chapter5.py
def get_coach_data(filename): try: with open(filename) as f: data = f.readline() return ( data.strip().split(',')) except IOError as err: print('File err:' + str(err)) return(None) def sanitize(time_string): if '-' in time_string: splitter = '-' elif ':' in time_string: splitter = ':' else: return(time_string) (mins, secs) = time_string.split(splitter) return(mins + '.' + secs) try: julie = get_coach_data('julie.txt') james = get_coach_data('james.txt') sarah = get_coach_data('sarah.txt') mikey = get_coach_data('mikey.txt') print( sorted( set ([sanitize(s) for s in julie]) )[0:3] ) print( sorted( set ([sanitize(s) for s in james]) )[0:3] ) print( sorted( set ([sanitize(s) for s in sarah]) )[0:3] ) print( sorted( set ([sanitize(s) for s in mikey]) )[0:3] ) except IOError as err: print("file error" + str(err))
![](http://images.cnitblog.com/i/578570/201406/181531547395370.png)
相关文章推荐
- Head First Python(推导数据)
- Python初入门(五)(Head First Python 第五章 处理数据)
- Head First Python 第二章 函数模块&第三章 文件与异常&第四章 持久存储&第五章 处理数据
- HeadFirstPython---------(三)第五章(推倒数据__处理数据)
- 《Head_First_Python》 5推导数据
- CGI-Web服务器接收并显示数据 《Head First Python》第九章
- 《Head First Programming》---python 2_文本数据
- Head First Python(管理你的数据)
- Head First Python 学习笔记-Chapter4:持久化--将数据写入文件
- [Head First Python]6. 定制数据对象:打包代码与数据
- HeadFirstPython---------(四)第六章(定制数据对象__打包代码与数据)
- Head Frist Python 读书笔记 第五章 处理数据
- HeadFirstPython---------(二)第四章,持久存储,数据保存到文件
- 《head first python》——定制数据对象
- 《Head First Programming》---python 4_文件和数组中的数据
- Head First Python ch_6 定制数据对象
- 数据存储:从pickle到sqlite 《Head First Python》第九章
- 《Head First Python》笔记 第九章 管理你的数据
- 【阅读】《head first jquery》第九章——处理JSON数据(getJSON不懂)
- Head First Python(处理复杂性)