删除包含字符串的行上方和下方的行数

时间:2013-07-10 19:59:18

标签: python python-2.5

我正在格式化GPS输出日志,我需要一种有效的方法来删除包含该行下方0和y行数的行上方的x行。

*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 0
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*

如果该行包含“Position fix ind:0”,请删除其上方的6行并删除下面的3行并删除它所在的行

编辑:

输入文件是.log文件

编辑2:

输入文件

1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 0
     Satellites Used: 3
        MSL Altitude: 00.0
 *--------------------------------------*
3
2
1
1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 5
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*
3
2
1

4 个答案:

答案 0 :(得分:3)

def remLines(infilepath, outfilepath, delim, above, below):
    infile = open(infilepath)
    outfile = open(outfilepath, 'w')
    buff = []
    line = infile.readline()
    while line:
        if line.strip() == delim:
             buff = []
             for _ in range(below): # need to error check here, if you're not certain that your input file is correctly formatted
                 infile.readline()
        else:
            if len(buff) == above:
                outfile.write(buff[0])
                buff = buff[1:]
            buff.append(line)
        line = infile.readline()
    outfile.write(''.join(buff))

if __name__ == "__main__":
    remLines('path/to/input', 'path/to/output', "Position fix ind: 0", 6,3)

<强>测试

输入:

1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 0
     Satellites Used: 3
        MSL Altitude: 00.0
 *--------------------------------------*
3
2
1
1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 5
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*
3
2
1

输出:

1
2
3
3
2
1
1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 5
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*
3
2
1

答案 1 :(得分:0)

您可以在此处使用set,对文件进行迭代,只要您在一行中看到'Position fix ind: 0'(例如,该行的索引为i),就会添加一个从i-6i+3的一组数字 一套。

f = open('abc')
se = set()
for i,x in enumerate(f):
    if 'Position fix ind: 0' in x:
        se.update(range(i-6,i+4))
f.close()

现在再次遍历文件并跳过该集合中存在的索引:

f = open('abc')
f1 = open('out.txt', 'w')
for i,x in enumerate(f):
    if i not in se:
        f1.write(x)
f.close()
f1.cose()

输入文件:

1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 0
     Satellites Used: 3
        MSL Altitude: 00.0
 *--------------------------------------*
3
2
1
1
2
3
*--------------------------------------*
            UTC Time: 000000.00
           Latitude: 0000.0000
            N/S ind.: N
           Longitude: 0000.0000
         E/W ind: E
    Position fix ind: 5
     Satellites Used: 3
        MSL Altitude: 00.0
*--------------------------------------*
3
2
1

<强>输出:

1
2
3
3
2
1
1
2
3
*--------------------------------------*
UTC Time: 000000.00
Latitude: 0000.0000
N/S ind.: N
Longitude: 0000.0000
E/W ind: E
Position fix ind: 5
Satellites Used: 3
MSL Altitude: 00.0
*--------------------------------------*
3
2
1

答案 2 :(得分:0)

如果文件不是太大:

 import re
 p = re.compile(r'(?:.*\n){6}\s*Position fix ind: 0\n(?:.*\n){3}')
 with open('test.txt') as f:
    output = p.sub('', f.read())

答案 3 :(得分:0)

我需要@ inspectorG4dget提供的内容,为此我要表示感谢。但是我需要对2500多个文件进行更改,然后自己对原始文件进行更改。我添加了一个额外的功能来处理该问题。 list.txt包含要进行更改的文件的名称,并且temp / tempfile用于临时写入。

from shutil import copyfile

def remLines(infilepath, outfilepath, delim, above, below):
    infile = open(infilepath)
    outfile = open(outfilepath, 'w')
    buff = []
    line = infile.readline()
    while line:
        if line.strip() == delim:
            buff = []
            for _ in range(below):
                infile.readline() 
        else:
            if len(buff) == above:
                outfile.write(buff[0])
                buff = buff[1:]
            buff.append(line)
        line = infile.readline()
    outfile.write(''.join(buff))


def readfiles(listfilepath, tempfilepath):
    refile = open(listfilepath)
    line = refile.readline()
    while line:
        realfilepath = line.strip()
        remLines(realfilepath, tempfilepath, 'This is test line 17', 2,7)
        copyfile(tempfilepath, realfilepath)
        line = refile.readline()

if __name__ == "__main__":
    readfiles('list.txt', 'temp/tempfile')