0

我正在编写一个程序,它基本上从日志文件中提取一行,解析它,并以简化的形式返回解析的数据。我目前的主要问题是我应该解析我的日期时间搅拌的方法。这是日志中的一行示例。

日志文件示例:

2012-06-12 14:02:16,341 [main] INFO ---
2012-06-12 14:02:16,509 [main] INFO ---
2012-06-12 14:02:17,000 [main] INFO ---
2012-06-12 14:02:17,112 [main] INFO ---
2012-06-12 14:02:20,338 [main] INFO ---
2012-06-12 14:02:21,813 [main] INFO ---

我解析 SO FAR 的代码(非常粗糙):

class LogLine:

    SEVERITIES = ['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG']
    severity = 1

    def __init__(self, line):
        try:
            t, s, self.filename, n, self.message =
                re.match(r"^(\d\d\d\d-\d\d-\d\d[ \t]\d\d:\d\d:\d\d,\d\d\d)", line)
            self.line = int(n)
            self.sev = self.SEVERITIES.index(s)
            self.time = time.strptime(t)


    def get_t(self):
        return

    def get_severity(self):
        return self.SEVERITIES.index(self)
    def get_message(self):
        return
    def get_filename(self):
        return
    def get_line(self):
        return

所以基本上(如果你不能从我糟糕的代码中推断出来)我正在使用正则表达式解析字符串以获取日期时间。我也一直在阅读有关 strptime 作为解决此问题的可能方法。最终,我需要将日期时间解析为毫秒,然后将其添加到日期时间中的毫秒整数(以逗号分隔)

我确信这个问题非常令人费解,我提前道歉。谢谢您的帮助。

4

2 回答 2

0
>>> datetime.datetime.strptime('2012-06-12 14:02:16,341' + '000', '%Y-%m-%d %H:%M:%S,%f')
datetime.datetime(2012, 6, 12, 14, 2, 16, 341000)
于 2012-07-09T21:20:37.857 回答
0

以下是如何解析一行的示例:

>>> # A line from the logfile.
>>> line = "2012-06-12 14:02:16,341 [main] INFO ---"
>>> # Parse the line.
>>> m = re.match(r"^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}),(\d{3}) \[([^]]*)\] (\S+) (.*)", line)
>>> timestamp, line_number, filename, severity, message = m.groups()
>>> # Show the various captured values.
>>> timestamp
'2012-06-12 14:02:16'
>>> line_number
'341'
>>> filename
'main'
>>> severity
'INFO'
>>> message
'---'
于 2012-07-09T23:26:57.737 回答