题目:

1、写一个程序,判断2008年是否是闰年。

2、写一个程序,用于计算2008年10月1日是这一年的第几天?(2008年1月1日是这一年的第一天)

3、(文件题)有一个“record.txt”的文件,内容如下:

# name, age, score

tom, 12, 86

Lee, 15, 99

Lucy, 11, 58

Joseph, 19, 56

第一栏为姓名(name),第二栏为年纪(age),第三栏为得分(score)

现在,写一个Python程序,

1)读取文件

2)打印如下结果:

得分低于60的人都有谁?

谁的名字以L开头?

所有人的总分是多少?

3)姓名的首字母需要大写,该record.txt是否符合此要求? 如何纠正错误的地方?

4、(练习正则表达式)有一个文件,文件名为output_1981.10.21.txt 。下面使用Python: 读取文件名中的日期时间信息,并找出这一天是周几。将文件改名为output_YYYY-MM-DD-W.txt (YYYY:四位的年,MM:两位的月份,DD:两位的日,W:一位的周几,并假设周一为一周第一天)

以下是程序清单:

 #-*-coding:utf-8 -*-

 # (1) judge leap year
def judge_leap_year(n):
# if n%4 == 0 and n%100 != 0:
# return True
# if n%100 == 0 and n%400 == 0:
if (n%4 == 0 and n%100 != 0) or (n%100 == 0 and n%400 == 0):
return True
else:
return False # ===================================================================
# (2) computing the sum days of any day(**.**.**)
def compute_year_counts(datestr):
# deal with these case:
# 2012.12.2
# 2012/12/2
# 2012-12-2
if datestr.find('.') > 0: date = datestr.split('.')
if datestr.find('/') > 0: date = datestr.split('/')
if datestr.find('-') > 0: date = datestr.split('-') year = int(date[0])
month = int(date[1])
day = int(date[2])
if (month < 1 or month > 12):
print "the error month!"
return -1
if (day > 31):
print "the error day!"
return -1 days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # nDays = 0
# i = 1
# while i < month:
# nDays += days[i-1]
# i = i + 1
nDays = sum(days[i] for i in range(0, month - 1))
if (judge_leap_year(year)):
nDays += 1
return nDays + day datestring = raw_input("input the datetime info:-->")
print compute_year_counts(datestring) # ===================================================================
# (3) read and write file: use class if perfect!
class UserProfier(object):
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score def read_file_and_anlysis(filetext):
line_read = []
user_scores = []
has_invalid_name = False #the invalid name for line in filetext:
if not line.startswith('#') and len(line.strip()) != 0:
if line[0].islower():
line = line[0].upper() + line[1:]
has_invalid_name = True
cur = line.strip().split(', ')
user_scores.append(UserProfier(cur[0], int(cur[1]), int(cur[2])))
line_read.append(line)
# print the file
print "print the file"
for i in line_read:
print i
# statistic the score < 60
print "users whose score lower 60:"
for scores in filter(lambda s: s.score < 60 , user_scores):
print scores.name
# statistic the begin of name which is 'L'
print "users whose name's begin is 'L':"
for names in filter(lambda s: s.name.startswith('L'), user_scores):
print names.name
# statistic the total scores of all one
print "the total scores of everyone:"
allscores = map(lambda s:s.score, user_scores)
print allscores
print reduce(lambda x, y: x+y, allscores, 0) # open the file
with open("record.txt") as f:
read_file_and_anlysis(f) # ===================================================================
# (4) the useful example of regular expression
import os, re, datetime filename = "output_1981.10.21.txt" date_time = re.search("(?P<year>\d{4})\.(?P<month>\d{2})\.(?P<day>\d{2})\.", filename) year = date_time.group('year')
month = date_time.group('month')
day = date_time.group('day') print year, month, day
date = datetime.date(int(year), int(month), int(day))
w = date.weekday() + 1
W = str(w)
os.rename(filename, "output_"+year+'-'+month+'-'+day+'-'+str(w)+".txt")

最新文章

  1. 修复一个吉日嘎拉MSSQL数据库版中的分页存储过程bug
  2. Apache commons-codec笔记
  3. cut用法
  4. 简单的MySQLDB类
  5. 【JS】Beginner3 &amp; 4 &amp; 5 &amp; 6:Maths &amp; Logic &amp; Conditonal &amp; Looping
  6. IIS7、IIS6 web.config注册HttpModule
  7. SQL使用单引号
  8. python去掉html标签
  9. redis新手入门,摸不着头脑可以看看&lt;二&gt;
  10. [bzoj4908][BeiJing2017]开车
  11. winfrom程序文本框第一次选中问题
  12. 文件编码检测.ZC一些资料(包含java的)
  13. Java桌面程序打包成exe可执行文件
  14. div+css布局记扎
  15. idea中添加类和方法注释以及codeCheck
  16. ArcGIS鼠标滚轮方向之注册表篇
  17. 高效开发iOS系列 -- 那些不为人知的KVC
  18. php--------使用js生成二维码
  19. Linux下C/C++代码调用PHP代码(转)
  20. cloudstack api调用python

热门文章

  1. 了解各种不同意义上的new
  2. Unity 2018 By Example 2nd Edition
  3. NoSql 数据库理解
  4. 事件委托在ios下面失效
  5. Android开发中同时存在多个ListView的处理
  6. css杂烩
  7. 微信H5开发
  8. 电子商务系统+java+web+完整项目+包含源码和数据库Java实用源码
  9. ubuntu中运行java程序
  10. 家庭家长本-微信小程序