python如何打印时间,在python2.7中,如何提取和打印日期、时间和m
代碼如下:#!/usr/bin/env python
#Import the datetime
from datetime import datetime
import re
#Create two datetime object for limit 1 and limit 2 as dt1 and dt2 respectively
dt1 = datetime.strptime("01:00:00","%H:%M:%S").time()
dt2 = datetime.strptime("04:59:59","%H:%M:%S").time()
#Create a compiler for regular expression
init_re = re.compile(r'(INIT)')
time_re = re.compile(r'(\d+:\d+:\d+)')
# read line from test.log file
for line in open("test.log", "r"):
match = time_re.search(line) #Search time format for each line
if match:
matchtime = match.group(1)
dt_match = datetime.strptime( matchtime, '%H:%M:%S').time()
#Time formmat match
if dt_match >= dt1 and dt_match <= dt2:
match1 = init_re.search(line) #search INIT format
if match1:
matchinit = match1.group(0)
print match.string.strip()
以下是日志文件的一部分:2015-12-15 00:51:01,904 INFO restser.py 113 [INIT] [netkv_restser: peek] [req_id: f0aa7ab5-6192-4231-93cd-82a53936a072] [request: {u'key_space': u'martech_user_index', u'table_name': u'nettopic', u'key': 9569}]
我想要這樣的輸出:
^{pr2}$
示例:[Date:2015-12-15 ] [Time:00:51:01,904 ] [INIT] [netkv_restser:peek ] [req_id:f0aa7ab5-6192-4231-93cd-82a53936a072 ]
注意:如果您有足夠的能力編輯代碼,請(qǐng)?zhí)峁┙鉀Q方案。我不想聽(tīng)上去粗魯無(wú)禮,但這讓我討厭。在
注意:我使用的是python2.7.6。在
總結(jié)
以上是生活随笔為你收集整理的python如何打印时间,在python2.7中,如何提取和打印日期、时间和m的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php7 echo无法显示,PHP7 e
- 下一篇: 开户oracle监听,R12:银行账户开