1

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

*--------------------------------------*
            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

4 回答 4

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
于 2013-07-10T20:06:28.363 回答
0

您可以在set此处使用 a ,遍历文件,并在看到'Position fix ind: 0'一行时立即(例如,该行的索引为i),然后将一组数字从i-6to添加i+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
于 2013-07-10T20:07:37.353 回答
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())
于 2013-07-10T20:39:27.327 回答
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')
于 2018-06-27T05:51:28.123 回答