Topic:
1, write a program to determine whether 2008 is a leap year.
2. Write a program to calculate October 1, 2008 is the day of the year? (January 1, 2008 is the first day of the year)
3, (file title) there is a "record.txt" file, the contents are as follows:
# name, age, score 19,56,.
The first column is the name, the second column is age, and the third column is the score (score)
Now, write a python program,
1) Read the file
2) Print the following results:
Who has scored less than 60 people?
Whose name begins with L?
What is the total score for all?
3) The first letter of the name needs to be capitalized, does the record.txt meet this requirement? How to correct the wrong place?
4, (exercise regular expression) has a file, the file name is Output_1981.10.21.txt. Use Python below: Read the datetime information in the file name and find out what the day is. Rename the file to Output_yyyy-mm-dd-w.txt (YYYY: Four-bit year, MM: two-bit month, DD: Two-bit day, W: one-digit week, and assumes Monday is the first day of the week)
The following is a list of the programs:
1 #-*-coding:utf-8-*-2 3 #(1) Judge Leap year4 defjudge_leap_year (n):5 #if n%4 = = 0 and n%100! = 0:6 #return True7 #if n%100 = = 0 and n%400 = = 0:8 if(n%4 = = 0 andN%100! = 0)or(n%100 = = 0 andn%400 = =0):9 returnTrueTen Else: One returnFalse A - # =================================================================== - #(2) Computing the sum days of any day (**.**.**) the defcompute_year_counts (DATESTR): - #deal with these case: - #2012.12.2 - #2012/12/2 + #2012-12-2 - ifDatestr.find ('.') > 0:date = Datestr.split ('.') + ifDatestr.find ('/') > 0:date = Datestr.split ('/') A ifDatestr.find ('-') > 0:date = Datestr.split ('-') at -Year =Int (date[0]) -month = Int (date[1]) -day = Int (date[2]) - if(Month < 1orMonth > 12): - Print "The error month!" in return-1 - if(Day > 31): to Print "The error day!" + return-1 - thedays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] * $ #ndays = 0Panax Notoginseng #i = 1 - #While i < month: the #ndays + = days[i-1] + #i = i + 1 ANdays = SUM (days[i] forIinchRange (0, month-1)) the if(Judge_leap_year (year)): +Ndays + = 1 - returnNdays + Day $ $datestring = Raw_input ("input the datetime info:-->") - Printcompute_year_counts (datestring) - the # =================================================================== - #(3) Read and write File:use class if perfect!Wuyi classUserprofier (object): the def __init__(self, name, age, score): -Self.name =name WuSelf.age = Age -Self.score =score About $ defread_file_and_anlysis (filetext): -Line_read = [] -User_scores = [] -Has_invalid_name = False#The invalid name A + forLineinchFiletext: the if notLine.startswith ('#') andLen (Line.strip ())! =0: - ifline[0].islower (): $line = Line[0].upper () + line[1:] theHas_invalid_name =True thecur = Line.strip (). Split (', ') theUser_scores.append (Userprofier (cur[0], int (cur[1]), int (cur[2]))) the Line_read.append (line) - #Print the file in Print "Print the file" the forIinchLine_read: the PrintI About #statistic the score < the Print "users whose score lower:" the forScoresinchFilterLambdaS:s.score < 60, user_scores): the PrintScores.name + #statistic the begin of name which is ' L ' - Print "users whose name ' s begin is ' L ':" the forNamesinchFilterLambdaS:s.name.startswith ('L'), user_scores):Bayi PrintNames.name the #statistic The total scores of all one the Print "The total scores of everyone:" -Allscores = Map (LambdaS:s.score, User_scores) - PrintAllscores the PrintReduceLambdaX, y:x+y, allscores, 0) the the #Open the file theWith open ("Record.txt") as F: - read_file_and_anlysis (f) the the # =================================================================== the #(4) The useful example of regular expression94 ImportOS, RE, datetime the thefilename ="Output_1981.10.21.txt" the 98Date_time = Re.search ("(? P<YEAR>\D{4}) \. (? P<MONTH>\D{2}) \. (? P<DAY>\D{2}) \.", filename) About -Year = Date_time.group (' Year')101month = Date_time.group ('Month')102Day = Date_time.group (' Day')103 104 PrintYear , month, Day theDate =datetime.date (int (year), Int. (month), Int (day))106W = date.weekday () + 1107W =Str (w)108Os.rename (filename,"Output_"+year+'-'+month+'-'+day+'-'+str (W) +". txt")
A few small exercises on the basics of Python