将结束日期保存在.txt文件下,然后取出每一行的日期,逐次进行操作,操作步骤如下: 1.先判断日期是否符合要求:如结束日期是否在开始日期之后,闰年的2月有29天,只有1-12个月等等.判断若不通过,直接输出错误结果,并将错误结果保存在文件中,若通过要求进入操作2. 2.计算开始日期到结束日期的天数:先用正则表达式将日期的年月日分别匹配出来,并转化成int格式.将月份的天数放入mouth_li [31,28,31,30,31,30,31,31,30,31,30,31]中 年份的天数放入year_li=[366,365,365,365]中,分别计算出 s0 :初始天到月末天数 s1 :结束天到月初天数 s2 :结束天的上一月到年初天数 s3 :初始月的下一月到年末天数 s4 :整年天数 在这过程中要是闰年的2月时,天数加1.当开始天数和结束天使在同一年时 sum = s0+s1+s2+s3-year_li[year%4] 当不在同一年时sum = s0+s1+s2+s3+s4.把sum值传入操作3中. 3.判断打鱼还是晒网,并将结果存入文件. 4.代码如下 import re def result(sum,data):#判断结果 i = sum%5 if i ==0 or i==4: print('晒网') f = open('result.txt', 'a', encoding='utf8') data = data.strip() + ' 晒网' f.write(data + '\t\n') f.close() else: print('打鱼') f = open('result.txt', 'a', encoding='utf8') data = data.strip()+' 打鱼' f.write(data+'\t\n') f.close() # def get_date():#从文件获取日期 # with open('1.txt','r',encoding='utf8') as f: # d=f.readline() # return d def judge_date(start_date,end_date):#判断日期是否正确 try: ret = re.findall('\d+', start_date) year0 = int(ret[0]) mouth0 = int(ret[1]) day0 = int(ret[2]) ret = re.findall('\d+', end_date) year = int(ret[0]) mouth = int(ret[1]) day = int(ret[2]) except Exception: exit() mouth_li = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if mouth>12 or mouth<1 or day<1 or year<year0: return False if year % 4 == 0 and year % 100 != 0 and mouth==2: if day<=29: return True elif day>mouth_li[mouth-1]: return False else: return True def sum(start_date,end_date):#计算天数 ret = re.findall('\d+',start_date)##取出开始的 年 月 日 year0 = int(ret[0]) mouth0 = int(ret[1]) day0 = int(ret[2]) ret = re.findall('\d+', end_date)#取出结束的年 月日 year = int(ret[0]) mouth = int(ret[1]) day = int(ret[2]) mouth_li = [31,28,31,30,31,30,31,31,30,31,30,31] year_li=[366,365,365,365] s0 = 1 #初始天到月末天数 s1 = day #结束天到月初天数 s2 = 0 #结束天的上一月到年初天数 s3 = 0#初始月的下一月到年末天数 s4 = 0# 整年天数 if mouth0==2:#初始天到月末天数 if year0%4==0 & year0%100!=0: s0 = mouth_li[1]+1-day0 else: s0 = mouth_li[mouth0-1]-day0 if mouth>2:#结束天的上一月到年初天数 for i in range(0,mouth-1): s2+=mouth_li[i] if year%4==0 & year0%100!=0: s2 =s2+1 if mouth==2: s2 = mouth_li[0] if mouth0==1:#初始月的下一月到年末天数 for i in range(mouth0, 12): s3 += mouth_li[i] if year0%4 and year0%100!=0==0: s3 = s3+1 elif mouth0>1: for i in range(mouth0, 12): s3 += mouth_li[i] if year-year0>=2: for i in range(year0+1,year): s4+=year_li[i%4] if mouth0==mouth and year == year0 and day== day0: return 1 if year==year0: s5 = s0+s1+s2+s3-year_li[year%4] # print('初始天到月末天数',s0) # print('结束天到月初的天数',s1) # print('结束月的上一月到年初天数',s2) # print('初始月的下一月到年末天数',s3) # print('整年天数',s4) # print('同一年差的天数',s5) return s5 else: return s0+s1+s2+s3+s4 # print(sum('2010年1月4日','2011年6月4日')) # sum('2011年1月4日','2011年5月4日') start_date = '2010年1月1日' with open('1.txt','r',encoding='utf8') as f: for data in f.readlines(): end_date = data.strip() print(end_date) if judge_date(start_date,end_date): count = sum(start_date,end_date) print('共计天数:',count) result(count,end_date) else: print('输入日期错误!') w = open('result.txt','a',encoding='utf8') d= end_date.strip()+' 输入日期错误!' w.write(d+'\t\n') w.close() # end_date = get_date() # if judge_date(end_date): # count = sum(start_date, end_date) # result(count, end_date) # else: # print('输入日期错误!')
三天打鱼两天晒网
最新推荐文章于 2021-03-22 18:33:00 发布