Exercises Vamei Source: http://www.cnblogs.com/vameihttp://www.cnblogs.com/vamei/archive/2012/07/19/2600135.html#undefined
"Implement" #!/usr/bin/python#-*-coding:utf-8-*- #-------Calculate date difference ' Import datetimefrom datetime import dated1= Date (2008,1,1) d2=date (2008,10,1) t=abs (d2-d1) n=t.days+1print n #----Determine if 2008 is a leap year, Def leapyear (year): #判断year是否为闰年, ture for leap year, false for non-leap year years=year*1 if (years%100): # #不能整除100进入条件语句 return not (years%4) #能整除4返回ture, cannot divide by 4, return false else: # #能整除100 return not (years%400) #能整除4返回ture, cannot be divisible by 4, return false #a =leapyear (2000) #print (Leapyear), leapyear (+), Leapyear (1900), Leapyear (1833)) ' # #----file Processing # main list index and read string split/ Strip and index reading; You can also use zip to make a list of names, ages, fractions, and then use the for (A, b) in S.numerate to read index that satisfies the type of the condition and then read the required output type # because there is a need to correct the error where all the files need to be opened two times, A copy used to read, a copy to rewrite, it is necessary to note that does not need to correct the line + skipped place to write the original content import Os,sys#import linecachereload (SYS) sys.setdefaultencoding (' Utf-8 ') filepatH= ' e:\ record. txt '. Decode (' Utf-8 '). Encode (' GB2312 ') # #打开windows中文路径解析 with Open (filepath, ' r+ ') as F1: f1.seek (0) l = f1.readlines () with open (filepath, ' w+ ') as F: sum=0 n=0 a=0 F.seek (0) # #读取文件时光标位置重新回到头部, otherwise easy to read to the empty print ' The all contents is:\n ', F.read () f.seek (0) for line in l: if (Line.isspace ()): # #处理文件为空行的数据 line=line f.write (line) continue if (line[0]== ' # '): # #忽略带 # start line or use if (not Line.startswith (' # ')) line=line f.write (line) continue a=a+1 l1=line.split (', ') l1[2] = L1[2].strip (' \ n ') #去除尾部的 \ n if (l1[2]*1<60): print ' whose score is lower than: ',l1[0] if (l1[0][0]== ' L '): print ' whose name begins with L: ', l1[0] sum = sum + int (l1[2 ]) if (not L1[0].istitle ()): & Nbsp; n=n+1 line=line.capitalize () f.write (line) print ' the Totalscore is ', Sumprint ' the name of the file is all title: ', (n==0)
Python exercises and implementations--file processing, date dates