【发布时间】:2019-08-21 12:57:45
【问题描述】:
我的日志文件如下。需要找到具有 GET 并在 2018-07-11 10 和 2018-07-11 11 之间运行的行
2018-07-11 10:12:06 12.115.14.240 "PUT /apng/assembler-2.0/assembler2.php HTTP/1.1" 403 231 "http://littlesvr.ca/apng/history.html"
2018-07-11 10:15:06 202.167.250.99 "GET /apng/images/o_sample.png?142475198 HTTP/1.1" 403 115656 "http://bbs.mydigit.cn/read.php"
2018-07-11 11:16:06 120.115.144.240 "GET /apng/assembler-2.0/assembler2.php HTTP/1.1" 200 231 "http://littlesvr.ca/apng/history.html"
with open(log) as f:
log = f.read()
###if condition to show to get GET
line = re.search(rx,log)
想要输出
2018-07-11 10:15:06 202.167.250.99 "GET /apng/images/o_sample.png?142475198 HTTP/1.1" 403 115656 "http://bbs.mydigit.cn/read.php"
【问题讨论】:
-
不要使用
log = f.read()。使用for line in f:,然后使用if '"GET ' in line: print(line)。
标签: python regex file regex-lookarounds regex-group