从零学python系列之数据处理编程实例(一)
时间:2021-10-05 12:36:43
要求:分别以james,julie,mikey,sarah四个学生的名字建立文本文件,分别存储各自的成绩,时间格式都精确为分秒,时间越短成绩越好,分别输出每个学生的无重复的前三个最好成绩,且分秒的分隔符要统一为“.”
数据准备:分别建立四个文本文件
james.txt 2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
julie.txt 2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
mikey.txt 2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
sarah.txt 2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
代码实现:
import os
os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter5') #将工作空间修改为文件所在的目录
#定义函数get_filedata从文件中取值
def get_filedata(filename):
try:
with open(filename) as f: #with语句打开和自动关闭文件
data=f.readline() #从文件中逐行读取字符
return (data.strip().split(',')) #将字符间的空格清除后,用逗号分隔字符
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]) #返回列表前三项
# 分别输出对应文件中排名前三的不重复的时间成绩
print (get_prev_three("james.txt"))
print (get_prev_three("julie.txt"))
print (get_prev_three("mikey.txt"))
print (get_prev_three("sarah.txt"))
输出结果:
['2.01', '2.22', '2.34']
['2.11', '2.23', '2.59']
['2.22', '2.38', '2.49']
['2.18', '2.25', '2.39']
标签:python,数据编程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pip安装tensorflow的坑的解决
2022-02-07 13:26:32
![](https://img.aspxhome.com/file/2023/4/107934_0s.png)
Python socket 套接字实现通信详解
2023-05-16 04:18:08
Python冲顶大会 快来答题!
2022-05-27 08:10:56
TensorFlow神经网络学习之张量与变量概念
2023-07-06 20:58:02
![](https://img.aspxhome.com/file/2023/1/79591_0s.png)
asp如何正确显示数据库里同时存在的GB码和BIG5编码?
2010-06-28 18:26:00
CSS实例教程:复合型CSS条状图表(上)
2010-01-23 12:43:00
![](https://img.aspxhome.com/file/UploadPic/20101/23/2010123124715558s.jpg)
详解Python字典查找性能
2022-05-06 10:45:34
使用Python手工计算x的算数平方根,来自中国古人的数学智慧
2021-12-07 01:29:53
![](https://img.aspxhome.com/file/2023/6/107666_0s.png)
PHP中流的定义及作用详解
2023-05-31 11:33:59
Mootools常用方法扩展(三)
2009-01-14 20:07:00
Python+Opencv身份证号码区域提取及识别实现
2021-10-01 17:32:13
![](https://img.aspxhome.com/file/2023/9/112969_0s.jpg)
python的sorted函数及使用解析
2022-03-02 05:52:21
Ajax改造:使用Ajax和jQuery改进现有站点
2010-04-02 12:50:00
![](https://img.aspxhome.com/file/UploadPic/20104/2/1709330-53s.jpg)
FrontPage2002简明教程八:站点的管理
2008-09-17 11:36:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017113823118s.jpg)
Python多线程扫描端口代码示例
2021-09-26 10:32:39
详细讲解SQL Server数据库的文件恢复技术
2009-01-15 12:54:00
Facebook是如何设计的[译]
2009-09-17 13:10:00
使用Python实现管理系统附源码
2023-04-04 04:22:34
![](https://img.aspxhome.com/file/2023/8/117588_0s.jpg)
Python3 实现文件批量重命名示例代码
2023-06-18 00:08:32
![](https://img.aspxhome.com/file/2023/7/72127_0s.jpg)
读"设计的3个C"之构图
2008-12-24 13:25:00