码迷,mamicode.com
首页 > 编程语言 > 详细

python学习(29)

时间:2019-01-10 11:24:48      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:格式   时间戳   timestamp   read   val   日期格   end   code   输入   

习题: 习题1:把一个文件中的所有数字删除 方式1: #encoding=utf-8 content_list = [] with open("e:\\a.txt") as file_obj: for line in file_obj: for c in line: if c.isdigit(): line=line.replace(c,"") content_list.append(line) with open("e:\\a.txt","w") as fp: fp.writelines(content_list) with open("e:\\a.txt") as fp: print(fp.read()) 方式2:老师 filtered_content="" with open("e:\\a.txt","r",encoding="utf-8") as fp: content=fp.read() for i in content: if i >="0" and i <= "9": continue else: filtered_content+=i with open("e:\\a.txt","w",encoding="utf-8") as fp: fp.write(filtered_content) with open("e:\\a.txt","r",encoding="utf-8") as fp: print(fp.read()) 习题2把一个多级目录中所有文件的字母删除 方式1: #encoding=utf-8 import os for root,dirs,files in os.walk("d:\\test"): for file in files: os.chdir(root) with open(file,encoding="utf8") as file_obj: new_line_list = [] for line in file_obj: for v in line: if v.isalpha(): line = line.replace(v,"") new_line_list.append(line) os.chdir(root) with open(file,"w",encoding="utf8") as fp: fp.writelines(new_line_list) 方式2:老师: import os.path file_path=[] for root,dirs,files in os.walk("e:\\letter"): for f in files: file_path.append(os.path.join(root,f)) print(file_path) for file in file_path: filter_content = "" with open(file) as fp: content = fp.read() for letter in content: if (letter >="a" and letter <="z") or (letter >="A" and letter <="Z"): continue else: filter_content+=letter with open(file,"w") as fp: fp.write(filter_content) 习题3:写函数或类,返回日期时间 当前的日期,格式:"xxxx年xx月xx日" 当前的时间,格式:"xx小时xx分钟xx秒" 当前的日期和时间,格式:xxxx年xx月xx日 xx小时xx分钟xx秒 把时间戳,转换为日期,格式:"xxxx年xx月xx日" 把时间戳,转换为日期和时间,格式:xxxx年xx月xx日 xx小时xx分钟xx秒 import time class DateTime(object): def __init__(object): pass def current_date(self): localtime = time.localtime() year = str(localtime[0]) month = str(localtime[1]) day = str(localtime[2]) return year + "年" + month + "月" + day + "日" def current_time(self): localtime = time.localtime() hour = str(localtime[3]) min = str(localtime[4]) sec = str(localtime[5]) return hour +"时" + min + "分" + sec + "秒" def current_date_time(self): localtime = time.localtime() year = str(localtime[0]) month = str(localtime[1]) day = str(localtime[2]) localtime = time.localtime() hour = str(localtime[3]) min = str(localtime[4]) sec = str(localtime[5]) return year + "年" + month + "月" + day + "日" + " " + hour +"时" + min + "分" + sec + "秒" def from_timestamp_to_date(self,timestamp): localtime = time.localtime(timestamp) year = str(localtime[0]) month = str(localtime[1]) day = str(localtime[2]) return year + "年" + month + "月" + day + "日" def from_timestamp_to_time(self,timestamp): localtime = time.localtime(timestamp) year = str(localtime[0]) month = str(localtime[1]) day = str(localtime[2]) localtime = time.localtime() hour = str(localtime[3]) min = str(localtime[4]) sec = str(localtime[5]) return year + "年" + month + "月" + day + "日" + " " + hour +"时" + min + "分" + sec + "秒" if __name__ == "__main__": date_time = DateTime() print(date_time.current_date()) print(date_time.current_time()) print(date_time.current_date_time()) print(date_time.from_timestamp_to_date(1254785455)) print(date_time.from_timestamp_to_time(1254785455)) 习题4:输入时间,计算距离1949年10月1日的天数 def is_leap_year(year): if (year%4==0 and year%100!=0) or year%400==0: return True else: return False def count_days_interval(): input_date=input( "请输入日期,格式:year-month-day:") if input_date.count("-")!=2: print( "输入日期格式有误,格式:year-month-day,请重新输入日期") return None year,month,day=input_date.split("-") if not (year.isdigit() and month.isdigit() and day.isdigit()): print( "输入日期必须为数字,格式:year-month-day,请重新输入日期:") year=int(year) month=int(month) day=int(day) init_year = 1949 init_month = 10 init_day = 1 if year-init_year==0: if month ==init_month: return day -init_day elif month -init_month==1: return 31+day-1 else: return 31+30+day-1 elif year-init_year==1: month_days=[31,28,31,30,31,30,31,31,30,31,30,31] leap_day=0 if month >2 and is_leap_year(year): leap_day=1 days =0 for i in month_days[:month-1]: days+=i return 92+leap_day+days+day else: month_days=[31,28,31,30,31,30,31,31,30,31,30,31] leap_day=0 if month >2 and is_leap_year(year): leap_day=1 days =0 for i in month_days[:month-1]: days+=i leap_days=0 for i in range(1950,year): if is_leap_year(i): leap_days+=1 years = year -init_year-1 return 92+leap_day+days+day+years*365+leap_days print(count_days_interval())

python学习(29)

标签:格式   时间戳   timestamp   read   val   日期格   end   code   输入   

原文地址:http://blog.51cto.com/13496943/2340900

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!