Prakash
Prakash

Reputation: 279

Find and remove lines in Python and output result

I have a large file. I want to remove some lines from it. I have found some other similar questions but they are not like this. The file looks like below:

A_B 3
Line 1
Line 2
Line 3

C_D 2
Another Line 1
Another line 2

A_B 1
Different line 1

Imagine large file with these lines only. First there will be constant string like A_B, C_D, E_G, etc then there will be variable number like A_B 3, C_D 4, etc It is followed by that numbers of line for example if there is A_B 2 then it will be followed by 2 lines. If there is A_B 3 then it will be followed by 3 lines. I want to remove "A_B (number)" itself and lines after that (number). In above, output should be:

C_D 2
Another Line 1
Another line 2

I have written script in Python which will print what I do not want :

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),

Upvotes: 0

Views: 441

Answers (4)

solurker
solurker

Reputation: 109

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

Upvotes: 3

Tony Babarino
Tony Babarino

Reputation: 3405

I am not sure that I have understood Your problem but I think that will do the job:

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()

Upvotes: 2

willnx
willnx

Reputation: 1283

This does the print you were trying to get within your test, doesn't remove the line.

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()

output:

C_D 2
Another Line 1
Another line 2

Upvotes: 1

Paul Cornelius
Paul Cornelius

Reputation: 10916

I don't know why you wrote a program to print everything you don't want. If I understand, this will print what you DO want:

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

Upvotes: 2

Related Questions