今天抽空练习了两道题,同时重点看了文件和输入输出章节基础知识。
1. 写一个函数判断输入日期为当年的第几天?
#deal with special case:
#2015-01-30
#2015.01.30
#2015/01/30
def year_of_date(datestr):
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])
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
nDays=sum(days[i] for i in range(0,month-1))+day
return nDays
- 打开一个文件:
fp=open('D:\Python27\Scripts\score.txt','r+')
for eachLine in fp:
print eachLine,
fp.close()