识别行和写入一个文件的功能(csv)

时间:2014-03-17 15:07:56

标签: python file csv lines identify

我有一个包含很多行的日志文件。日志文件采用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) 

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

2 个答案:

答案 0 :(得分:0)

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为真,请写入文件。

答案 1 :(得分:0)

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

# 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))