在Python中查找和删除行并输出结果

时间:2016-02-20 01:46:21

标签: python python-2.7

我有一个大文件。我想从中删除一些行。我发现了一些其他类似的问题,但他们不是这样的。该文件如下所示:

A_B 3
Line 1
Line 2
Line 3

C_D 2
Another Line 1
Another line 2

A_B 1
Different line 1

想象一下只有这些行的大文件。首先会有像A_B,C_D,E_G等一样的常量字符串,然后会出现像A_B 3,C_D 4等变量编号。接下来是行数,例如,如果有A_B 2那么它将跟随2线。如果有A_B 3那么它将跟随3行。我想删除“A_B(数字)”本身和之后的行(数字)。在上面,输出应该是:

C_D 2
Another Line 1
Another line 2

我用Python编写了脚本,它将打印出我不想要的内容:

with open('file.txt') as oldfile:
   for line in oldfile:
         if 'A_B' in line:
             number=line.split()[1]

             num=int(number)
             print
             print line,
             for i in range(num):
                 print next(oldfile),

4 个答案:

答案 0 :(得分:3)

unwanted_headers=['A_B']
skip_this_many=0

with open('file.txt','r') as oldfile:
    for line in oldfile:
        if not skip_this_many:
            for unwanted_header in unwanted_headers:
                if line.startswith(unwanted_header):
                    skip_this_many=int(line.split()[1])
                else:
                    print line
        else:
            skip_this_many -= 1

答案 1 :(得分:2)

我不确定我是否理解了您的问题,但我认为这样做可以做到:

f = open("file.txt", "r")
lines = f.readlines()
f.close()

f = open("file.txt", "w")
num = 0
for line in lines:
    if num > 0:
        num = num - 1
    elif 'A_B' in line:
        number = line.split()[1]
        num = int(number)
    else:
        f.write(line)
f.close()

答案 2 :(得分:2)

我不知道为什么你写了一个程序来打印你不想要的东西。如果我理解,这将打印你想要的东西:

with open('file.txt') as oldfile:
    skip = 0
    for line in oldfile:
        if skip > 0:
            skip -= 1
        elif 'A_B' in line:
            number = line.split()[1]
            skip = int(number)
        else:
            print line

答案 3 :(得分:1)

这是您尝试在测试中获得的打印件,但不会删除该行。

MYFILE = '/path/to/longfile.txt'

with open(MYFILE) as oldfile:

    line = oldfile.readline()
    while line:
        if line.startswith('A_B'):
           skip_counter = line.split()[1]
           for _ in xrange(int(skip_counter)):
               oldfile.readline()
        else:
            print line

        line = oldfile.readline()

输出:

C_D 2
Another Line 1
Another line 2