在连续行中满足多个条件时进行文件解析

时间:2018-05-01 15:20:44

标签: python for-loop fileparsing

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

============================= 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列中提取信息。
  4. 我可以做第三部分但是,我正在努力解决前两个问题,因为当我使用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
    

    请帮助我。

1 个答案:

答案 0 :(得分:0)

这应该做你想要的:

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