首页 > 解决方案 > 连续行满足多个条件时的文件解析

问题描述

我正在尝试解析具有如下数据的文本文件:

============================= condition 1 ============================

condition 2 string

col 1 col2 tags
------------------------------
      xx xx abc
      xx xx ac

col4 col 1       col5 col6        col7       col8     col9      col10     col11        col12   col13
-----------------------------------------------------------------------------------------------------
     1  11        6    30         abc        text    -2794      682         57388      294      210
     2  11        6    30         ac         text    -2447      680         52973      302      214
     3  11        13                         text    -2619     -805        120956      568      255
     4  11        16                         text     2185    -1261        116983      548      273
     5  11        17                         text    -3362    -1413        127136      569      278

Criterion 30 : xxxxx         text3 11 : some text here

============================================================================

以下是我想做的事情

  1. 查找条件 1,如果满足,请确保存在条件 2 字符串
  2. 选择“标签”列中的值
  3. 然后在下表中查找这些标签以从第 9 到 13 列中提取信息。

我可以做第三部分,但是,当我使用 f.next() 检查条件 2 时,我正在努力解决前两个问题,它弄乱了我的代码:

with open(each_file) as f:
    copy = False
    i = 0
    for linenum,line in enumerate(f):
        if line.strip() == "============================= Condition 1 ============================":
            line_next = f.next()
            if line_next.strip() == "condition 2 string":
                print "here1"
                print line.strip()
                copy = True ## Start copying when both the conditions are met

            elif line_next.strip() == "col4 col 1       col5 col6        col7       col8     col9      col10     col11        col12   col13": ## Stop copying at this line
                if i == 0:
                    copy = False
                else:
                    copy = False
                i = i + 1

        elif copy:
            print copy
            print line

请帮我解决一下这个。

标签: pythonfor-loopfileparsing

解决方案


这应该做你想要的:

with open(each_file) as f:
    cond_1 = False
    copy = False
    for linenum,line in enumerate(f.readlines()):
        line = line.strip()
        #print("DEBUG: line is <{0}>".format(line))
        if line == "============================= condition 1 ============================":
            print "DEBUG: condition 1"
            cond_1 = True

        elif cond_1 and line == "condition 2 string":
            print "DEBUG: condition 2 / start copying"
            copy = True ## Start copying when both the conditions are met

        elif line == "col4 col 1       col5 col6        col7       col8     col9      col10     col11        col12   col13": ## Stop copying at this line
            print "DEBUG: stop copying"
            copy = False

        if copy:
            #print "DEBUG: Copying..."
            print line

推荐阅读