Python - 在单行/多行片段中读取文件

时间:2013-04-01 19:36:57

标签: python

我对Python很陌生,我已经在这里找到了大部分问题的答案,但这个问题令我感到难过。

我正在使用Python处理日志文件,通常每行都以日期/时间戳开头,例如:

[1/4/13 18:37:37:848 PST]

在99%的情况下,我可以逐行阅读,查找感兴趣的项目并相应地处理它们但是偶尔日志文件中的条目将包含一个包含回车/新行字符的消息,因此它将跨越多行。

有没有办法可以轻松阅读文件"时间戳"这样当发生这种情况时,多行将合并为一个读取而不是?例如:

[1/4/13 18:37:37:848 PST] A log entry
[1/4/13 18:37:37:848 PST] Another log entry
[1/4/13 18:37:37:848 PST] A log entry that somehow
got some new line
characters mixed in
[1/4/13 18:37:37:848 PST] The last log entry

将被视为四行,而不是现在的六行。

提前感谢您的帮助。

克里斯,

...更新

myTestFile.log包含上面的确切文本,这是我的脚本:

import sys, getopt, os, re
sourceFolder = 'C:/MaxLogs'
logFileName = sourceFolder + "/myTestFile.log"
lines = []

def timestamp_split(file):
    pattern = re.compile("\[(0?[1-9]|[12][0-9]|3[01])(\/)(0?[1-9]|[12][0-9]|3[01])(\/)([0-9]{2})(\ )")
    current = []
    for line in file:
        if not re.match(pattern,line):
            if current:
                yield "".join(current)
            current == [line]
        else:
            current.append(line)
    yield "".join(current)

print "--- START ----"
with open(logFileName) as file:
    for entry in timestamp_split(file):
        print entry
        print "- Record Separator -"
print "--- DONE ----"

当我运行它时,我得到了这个:

--- START ----
[1/4/13 18:37:37:848 PST] A log entry
[1/4/13 18:37:37:848 PST] Another log entry
[1/4/13 18:37:37:848 PST] A log entry that somehow

- Record Separator -
[1/4/13 18:37:37:848 PST] A log entry
[1/4/13 18:37:37:848 PST] Another log entry
[1/4/13 18:37:37:848 PST] A log entry that somehow

- Record Separator -
[1/4/13 18:37:37:848 PST] A log entry
[1/4/13 18:37:37:848 PST] Another log entry
[1/4/13 18:37:37:848 PST] A log entry that somehow
[1/4/13 18:37:37:848 PST] The last log entry
- Record Separator -
--- DONE ----

我似乎在线上迭代了太多次,我期待(希望)是这样的:

--- START ----
[1/4/13 18:37:37:848 PST] A log entry
- Record Separator -
[1/4/13 18:37:37:848 PST] Another log entry
- Record Separator -
[1/4/13 18:37:37:848 PST] A log entry that somehow got some new line characters mixed in
- Record Separator -
[1/4/13 18:37:37:848 PST] The last log entry
- Record Separator -
--- DONE ----

正如评论中所讨论的那样,我在测试时不小心将 not 与正则表达式模式进行了比较,如果我将其删除,那么我得到的所有部分行让我更加困惑!

--- START ----
got some new line
characters mixed in

- Record Separator -
got some new line
characters mixed in

- Record Separator -
--- DONE ----

2 个答案:

答案 0 :(得分:2)

最简单的方法是实现一个简单的生成器来执行此操作:

def timestamp_split(file):
    current = []
    for line in file:
        if line.startswith("["):
            if current:
                yield "".join(current)
            current == [line]
        else:
            current.append(line)
    yield "".join(current)

当然,假设行开头的"["足以表示时间戳 - 您可能希望进行更重要的检查。

然后做一些像:

with open("somefile.txt") as file:
    for entry in timestamp_split(file):
        ...

(这里使用the with statement - 打开文件的好习惯。)

答案 1 :(得分:0)

import re

lines = []
pattern = re.compile('\[\d+/\d+/\d+\s\d+:\d+:\d+\s\w+\]')
with open('filename.txt', 'r') as f:
    for line in f:
        if re.match(pattern, line):
            lines.append(line)
        else:
            lines[-1] += line

这将时间戳与正则表达式匹配。可以根据需要进行调整。它还假定第一行包含时间戳。