head first python 6

简介: 使用函数 点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- #函数与处理的数据打包一起.
使用函数

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. #函数与处理的数据打包一起.
  4. def filetolist(file,listname):
  5.     try:
  6.         #打开文件
  7.         with open(file) as jaf:
  8.             #读取数据行
  9.             data = jaf.readline()
  10.         #转换成list
  11.         listname=data.strip().split(',')
  12.         data = {}
  13.         data['name'] = listname.pop(0)
  14.         data['dob'] = listname.pop(0)
  15.         data['time'] = listname
  16.         result = print(data['name'] + '的三次最佳成绩是' + str(sorted(set([sanitize(each_it) for each_it in data['time']]))[0:3]))
  17.         #return listname
  18.         return result
  19.     except IOError as ioerr:
  20.         print('File error : %s' % ioerr)
  21.         return(None)
  22.         
  23. #处理字符,转换成m.s格式
  24. def sanitize(time_string):
  25.     if '-' in time_string:
  26.         splitter = '-'
  27.     elif ':' in time_string:
  28.         splitter = ':'
  29.     else:
  30.         return time_string
  31.     (min, sec) = time_string.split(splitter)
  32.     return (min + '.' + sec)

  33. for name in ["james", "julie", "mikey", "sarah"]:
  34.     thelist=filetolist(name+".txt",name)
  35.     #使用列表
  36.     #username=name+'user'
  37.     #userdob =name+'dob'
  38.     #username = thelist.pop(0)
  39.     #userdob = thelist.pop(0)
  40.     ##使用列表推导式
  41.     #name2 = [sanitize(each_it) for each_it in thelist]
  42.     ##使用工厂函数set()
  43.     #try:
  44.     # print(username + '的最佳成绩是' + str(sorted(set(name2))[0:3]))
  45.     #except TypeError as typerr:
  46.     # print('list type error %s' % typerr)
  47.     #使用字典
使用类

点击(此处)折叠或打开

  1. #!/usr/bin/env python3
  2. # -*- coding:utf-8 -*-
  3. import os
  4. class athlete:
  5.     def __init__(self, athlete_name, athlete_dob=None, athlete_times=[]):
  6.         self.name = athlete_name
  7.         self.dob = athlete_dob
  8.         self.times= athlete_times
  9.     def top3(self):
  10.         return(sorted(set([sanitize(time) for time in self.times]))[0:3])

  11. def openfile(filename):
  12.     try:
  13.         #打开文件
  14.         with open(filename) as athlete_file:
  15.             #读取数据
  16.             data = athlete_file.readline()
  17.             value_list= data.strip().split(',')
  18.             username = value_list.pop(0)
  19.             userdob = value_list.pop(0)
  20.             usertimes= value_list
  21.             #返回实例对象
  22.             athlete_instance=athlete(username,userdob,usertimes)
  23.             return(athlete_instance)
  24.     except IOError as ioerr:
  25.         print('File error %s' % ioerr)
  26.         return(None)

  27. #处理字符,转换成m.s格式
  28. def sanitize(time_string):
  29.     if '-' in time_string:
  30.         splitter = '-'
  31.     elif ':' in time_string:
  32.         splitter = ':'
  33.     else:
  34.         return time_string
  35.     (min, sec) = time_string.split(splitter)
  36.     return (min + '.' + sec)
  37. for name in ["james", "julie", "mikey", "sarah"]:
  38.     name = openfile(name+'.txt')
  39.     print(name.name + '的三次最佳成绩是' + str(name.top3()))


t@localhost 6$ python3 kelly.py     
James Lee的三次最佳成绩是['2.01', '2.16', '2.22']
Julie Jones的三次最佳成绩是['2.11', '2.23', '2.59']
Mikey McManus的三次最佳成绩是['2.22', '2.31', '2.38']
Sarah Sweeney的三次最佳成绩是['2.18', '2.21', '2.22']
目录
相关文章
|
Web App开发 Python
Head First Python 7 web开发
t@localhost webapp$ tree . ├── cgi-bin │   ├── athletemodel.py │   ├── generate_list.py │   ├── generate_timing_data.
953 0
|
Python
head first python 5
点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- def filetolist(file,listna...
584 0
|
Python
head first python 6 class 扩展
点击(此处)折叠或打开 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os clas...
722 0
|
12天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
12天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
16天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。
|
1月前
|
编译器 测试技术 C++
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
165 0
|
5天前
|
安全 数据处理 开发者
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
18 1
|
5天前
|
人工智能 数据挖掘 程序员
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
34 0
|
6天前
|
API Python
Python模块化编程:面试题深度解析
【4月更文挑战第14天】了解Python模块化编程对于构建大型项目至关重要,它涉及代码组织、复用和维护。本文深入探讨了模块、包、导入机制、命名空间和作用域等基础概念,并列举了面试中常见的模块导入混乱、不适当星号导入等问题,强调了避免循环依赖、合理使用`__init__.py`以及理解模块作用域的重要性。掌握这些知识将有助于在面试中自信应对模块化编程的相关挑战。
18 0