求python脚本,从txt检索出特定字符的行(有很多行,行里面有记录的时间),并从行中抓出对应的时间字符

最后需要算出时间差

逐行匹配。对于每行可以首先使用find来确定该行中有没有特定字符。如果有,则根据正则表达式从中提取时间字符。

以下举一个例子,假设特定字符串为name,时间格式为xxxx-xx-xx。

def main():
    import re
    time_format = "\d+-\d+-\d+" #时间格式
    special_string = "name" #特定字符串
    pattern = re.compile(time_format)
    txt_content = open("test.txt", "r")
    for line in txt_content:
        l = line.strip()
        if l.find(special_string)>=0:   #如果有特定字符串
            print l                     #打印对应的行
            match =pattern.match(l)     #如果有匹配的时间格式
            if match:
                print match.group()     #打印对应的时间
if __name__ == '__main__':
    main()

样例test.txt为

2014-2-11 Your behavior is causing our name to be dragged through the mud. 
2014-2-12 I am so happy to get munificent birthday presents from my friends.
2014-2-13 He's the boss in name only, because I issue all the orders. 
2014-2-14 We are happy to give the product our full endorsement. 
2014-2-15 His name leaped out at me from the book. 
2014-2-16 We'll be happy to help if you need us.

输出结果为

2014-2-11 Your behavior is causing our name to be dragged through the mud.
2014-2-11
2014-2-13 He's the boss in name only, because I issue all the orders.
2014-2-13
2014-2-15 His name leaped out at me from the book.
2014-2-15

追问

21:57:41.059-[DEBUG]-[M1]-[D7]-LigeEND Test Block 1/12 - test start time:2013-12-18 21:57:16 ms - test end time:2013-12-18 21:57:41 ms

需要 D7 1/12 21:57:16 21:57:41

追答

修改一下字符串和时间格式即可。

def main():
    import re
    time_format = "\d+:\d+:\d+ " #时间格式
    special_string1 = "D7" #特定字符串
    special_string2 = "1/12" #特定字符串
    pattern = re.compile(time_format)
    txt_content = open("test.txt", "r")
    for line in txt_content:
        l = line.strip()
        if l.find(special_string1)>=0:      #如果有特定字符串1
            if l.find(special_string2)>=0:  #如果有特定字符串2
                match =pattern.findall(l)   #如果有匹配的时间格式
                if match:
                    for i in match:
                        print i             #打印对应的时间

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-02-18
你至少应该贴几行你文件的内容吧?
时间什么格式啊?追问

21:57:41.059-[DEBUG]-[M1]-[D7]-LigeEND Test Block 1/12 - test start time:2013-12-18 21:57:16 ms - test end time:2013-12-18 21:57:41 ms

需要 D7 1/12 21:57:16 21:57:41

追答>>> s = '21:57:41.059-[DEBUG]-[M1]-[D7]-LigeEND  Test Block 1/12 - test start time:2013-12-18 21:57:16 ms - test end  time:2013-12-18 21:57:41 ms'
>>> import re
>>> pat = re.compile('[\d:.]+-(?:\[([^\]]+)\]-)+\D+(\d+\/\d+)\D+[\d-]+ ([\d:]+)\D+[\d-]+ ([\d:]+)')
>>> m = pat.findall(s)
>>> m
[('D7', '1/12', '21:57:16', '21:57:41')]
>>> t1 = datetime.datetime.strptime(m[0][2], '%H:%M:%S')
>>> t2 = datetime.datetime.strptime(m[0][3], '%H:%M:%S')
>>> print t2 - t1
0:00:25

上面的可以一次提取出来,并计算出时间

追问

怎么先从TXT中抓出很多类似行的 s = '21:57:41.059-[DEBUG]-[M1]-[D7]-LigeEND Test Block 1/12 - test start time:2013-12-18 21:57:16 ms - test end time:2013-12-18 21:57:41 ms'

追答def processText():
f = open("1.txt", "r") #把1.txt改成你文件的名字
for s in f:
pat = re.compile('[\d:.]+-(?:\[([^\]]+)\]-)+\D+(\d+\/\d+)\D+[\d-]+ ([\d:]+)\D+[\d-]+ ([\d:]+)')
m = pat.findall(s)
print m
t1 = datetime.datetime.strptime(m[0][2], '%H:%M:%S')
t2 = datetime.datetime.strptime(m[0][3], '%H:%M:%S')
print t2 - t1

本回答被提问者采纳
相似回答