函数用于在一个文件(csv)中识别行和写入



我有一个日志文件,里面有很多行。日志文件为csv格式。我正在该文件中搜索不同的消息,我想将它们存储在另一个文件中。

我怎么能做到呢?

目前我是这样做的:

with open('/tmp/result/warnings/test/test3.csv', 'r') as input_file:
    with open('/tmp/result/warnings/test/test4.csv', 'w') as output_file:
        for line in input_file:
            if not "Failed to open output file" in line:
                output_file.write(line)
with open('/tmp/result/warnings/test/test4.csv', 'r') as input_file:
    with open('/tmp/result/warnings/test/test5.csv', 'w') as output_file:
        for line in input_file:
            if not "Invalid file length of" in line:
                output_file.write(line) 

我可以这样做,就像在一次查找几个消息,然后在一个文件中写入吗?

with open('/tmp/result/warnings/test/test3.csv', 'r') as input_file:
    with open('/tmp/result/warnings/test/test4.csv', 'w') as output_file:
    for line in input_file:
        conditions = [not "Failed to open output file" in line, not "Invalid file length of" in line]
        if all(conditions):
            output_file.write(line)

将允许您检查多个条件,如果它们是all true,则写入文件。

这是一个超模块化的版本:

# assumes Python 2.7
import csv
def read_csv(fname, header=False, **kwargs):
    with open(fname, "rb") as inf:
        incsv = csv.reader(inf, **kwargs)
        if header:
            head = next(incsv, [])
        for row in incsv:
            yield row
def write_csv(fname, rows, **kwargs):
    with open(fname, "wb") as outf:
        outcsv = csv.writer(outf, **kwargs)
        outcsv.writerows(rows)
def multi_filter(items, any_of=None, all_of=None):
    if any_of is None and all_of is None:
        return items
    if any_of is None:
        test_any = lambda item: True
    else:
        test_any = lambda item: any(cond(item) for cond in any_of)
    if all_of is None:
        test_all = lambda item: True
    else:
        test_all = lambda item: all(cond(item) for cond in all_of)
    return (item for item in items if test_any(item) and test_all(item))
def csv_filter(inf, outf, any_of=None, all_of=None):
    write_csv(outf, multi_filter(read_csv(inf), any_of, all_of))
conditions = [
    lambda row: not row[2].startswith("Failed to open output file"),
    lambda row: not row[2].startswith("Invalid file length of")
]
inf = '/tmp/result/warnings/test/test3.csv'
outf = '/tmp/result/warnings/test/test5.csv'
csv_filter(inf, outf, all_of(conditions))

相关内容

  • 没有找到相关文章

最新更新