Python 解析日志-如何读取部分行

Python 解析日志-如何读取部分行,python,parsing,Python,Parsing,我试图写一些东西来解析和报告一个非常大和详细的日志文件中非常具体的部分 基本上,结构可以描述为: 我不在乎的东西 我不在乎的东西 我不在乎的东西 更多我不在乎的东西 调试2015-03-13 01:20:03传输。py:200个新的传输候选者:set([“”]) 我不在乎的东西 我不在乎的东西 我不在乎的东西 更多我不在乎的东西 调试2015-03-13 01:20:03传输。py:200个新的传输候选者:set(['foo/bar'])) 还有很多我不在乎的东西 还有更多我不在乎的东西 还有更

我试图写一些东西来解析和报告一个非常大和详细的日志文件中非常具体的部分

基本上,结构可以描述为:

我不在乎的东西
我不在乎的东西
我不在乎的东西
更多我不在乎的东西
调试2015-03-13 01:20:03传输。py:200个新的传输候选者:set([“”])
我不在乎的东西
我不在乎的东西
我不在乎的东西
更多我不在乎的东西
调试2015-03-13 01:20:03传输。py:200个新的传输候选者:set(['foo/bar']))
还有很多我不在乎的东西
还有更多我不在乎的东西
还有更多我不在乎的东西
信息2015-03-13 09:00:01传输。副本:363状态信息:{u'status':u'COMPLETE',u'name':u'bar',u'path':u'unrelated content',u'directory':u'unrelated content',u'microservice':u'Remove the processing directory',u'message':u'Fetched status for 67646105-2c08-47ec-93d1-b7d3f3b43d13成功','u'type':u'SIP',uuid':u'67646105-2c08-47ec-93d1-B7D3B43D13'}
我要做的是逐行读取文件,并找到
新转学候选者的任何实例,其中
集合([''])
的内容不是空的。在这种情况下,我想获取字符串(在本例中为
'foo/bar'
)我还想把时间戳放在变量的那一行

当我继续逐行阅读时,我还想寻找包含
状态信息的行:{u'Status':u'COMPLETE
”,然后我想取“name”(即
u'name':u'bar'
)并将其放入变量(在本例中为
'bar'
)中。如上所述,我想将时间戳放入变量中

这里的目的基本上是看什么时候开始传输,以及传输何时完成。我写了一些可笑的基本废话:

#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser(description=
    "Python tool for generating performance statistics from Archivematica's "
    "Automation-tools log file")
parser.add_argument('-i', '--input', type=file, help='log file to read')
args = parser.parse_args()
if not (args.input):
    parser.error('you did not specify a log file')

log = args.input
x = 0
for line in log:
    if 'New transfer candidates' in line:
        x = x+1
print x
我的问题是,我真的不知道如何在这些行的不同部分找到我要查找的字符串?

使用标准库中的模块或开源模块

下面的示例演示如何使用
re
解析包含集合数据的行

#!/usr/bin/env python

import argparse
import re

parser = argparse.ArgumentParser(description="Python tool for generating performance statistics from Archivematica's Automation-tools log file")
parser.add_argument('-i', '--input', type=file, help='log file to read')
args = parser.parse_args()

if not (args.input):
    parser.error('you did not specify a log file')

log = args.input

x = 0
regex1 = re.compile("New transfer candidates: set\(\['(.+)'\]\)")
for line in log:
    if 'New transfer candidates' in line:
        m = regex1.search(line)
        if m:
            print m.group(1)
        x = x+1
print x
使用标准库中的模块或开源模块

下面的示例演示如何使用
re
解析包含集合数据的行

#!/usr/bin/env python

import argparse
import re

parser = argparse.ArgumentParser(description="Python tool for generating performance statistics from Archivematica's Automation-tools log file")
parser.add_argument('-i', '--input', type=file, help='log file to read')
args = parser.parse_args()

if not (args.input):
    parser.error('you did not specify a log file')

log = args.input

x = 0
regex1 = re.compile("New transfer candidates: set\(\['(.+)'\]\)")
for line in log:
    if 'New transfer candidates' in line:
        m = regex1.search(line)
        if m:
            print m.group(1)
        x = x+1
print x

这应该让你开始:

import time
import re
import ast

with open('input.txt') as logfile:
    for line in logfile:
        line = line.strip()
        # search for level and timestamp
        match = re.match(r'(\S+)\s+(\S{10} \S{8})\s*(\S.*)$', line)
        if match:
            level = match.group(1)
            timestr = match.group(2)
            timestamp = time.mktime(time.strptime(timestr, '%Y-%m-%d %H:%M:%S'))
            message = match.group(3)

            # transfer candidates
            match = re.match(r'.*New transfer candidates: set\((.*)\)', message)
            if match:
                candidates = ast.literal_eval(match.group(1))
                print 'New transfer candidate:', candidates
                continue

            # status info
            match = re.match(r'.*Status info: (.*)$', message)
            if match:
                info = ast.literal_eval(match.group(1))
                print 'Status info:', info
                continue

            print 'Unrecognized message.'
        else:
            print 'Unrecognized line.'

这应该让你开始:

import time
import re
import ast

with open('input.txt') as logfile:
    for line in logfile:
        line = line.strip()
        # search for level and timestamp
        match = re.match(r'(\S+)\s+(\S{10} \S{8})\s*(\S.*)$', line)
        if match:
            level = match.group(1)
            timestr = match.group(2)
            timestamp = time.mktime(time.strptime(timestr, '%Y-%m-%d %H:%M:%S'))
            message = match.group(3)

            # transfer candidates
            match = re.match(r'.*New transfer candidates: set\((.*)\)', message)
            if match:
                candidates = ast.literal_eval(match.group(1))
                print 'New transfer candidate:', candidates
                continue

            # status info
            match = re.match(r'.*Status info: (.*)$', message)
            if match:
                info = ast.literal_eval(match.group(1))
                print 'Status info:', info
                continue

            print 'Unrecognized message.'
        else:
            print 'Unrecognized line.'

最好的方法可能是通过模块来匹配您在文件中感兴趣的字符序列。最好的方法可能是通过模块来匹配您在文件中感兴趣的字符序列。可能是编译正则表达式的功劳的重复,但它是这在python中实际上是不必要的。它们被缓存了。太棒了–我不知道group方法。这正是我编译正则表达式所需要的,但在python中它实际上是不必要的。它们被缓存了。太棒了–我不知道group方法。这正是我需要的