#文本名称 weblog.txt
#文本内容
2019-05-15 08:10:01 aaaa
2019-05-15 08:32:00 bbbb
2019-05-15 09:01:02 cccc
2019-05-15 09:28:23 dddd
2019-05-15 10:42:58 eeee
2019-05-15 11:08:00 ffff
2019-05-15 12:35:03 gggg
#题目要求
从文件weblog.txt中提取9点至12点之间的文本内容
#解题思路
1-调用时间模块
2-定义变量
3-打开文本
4-for循环遍历文本内容
5-if判断锁定时间段
6-输入信息
#python代码
import time
t9=time.strptime('2019-05-15 09:01:02','%Y-%m-%d %H:%M:%S')
t12=time.strptime('2019-05-15 12:01:02','%Y-%m-%d %H:%M:%S')
with open('weblog.txt')as fobj:
for linein fobj:
t= time.strptime(line[:19],'%Y-%m-%d %H:%M:%S')
# if t9<=t<=t12:
# print(line,end='')
if t > t12:
break
if t >= t9:
print(line,end='')