Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting specific text from text file

I have a text files Text file

>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket

I wrote the following code for detecting sequence unavailable from the text file and write it in a new text file

lastline = None
with open('output.txt', 'w') as W:
    with open('input.txt', 'r') as f:
        for line in f.readlines():
            if not lastline:
                lastline = line.rstrip('\n')
                continue
            if line.rstrip('\n') == 'Sequence unavailable':
                _, _, id = lastline.split('|')
                data= 'Sequence unavailable|' + id
                W.write(data)
                W.write('\n')
            lastline = None

It work fine , it detect the sequence unavailabe from the text file and write it in a new file , but i want it to delete it from the file which it read from like

input.txt

>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket

input after code should be like this

>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|EKB
Cricket
like image 893
Rocket Avatar asked Mar 25 '26 19:03

Rocket


1 Answers

Here I am not using file.readlines method, as it fetches all the lines from the file into a list. So, it is not memory efficient.

Method 1: Using a temporary file.

import os
with open('input.txt') as f1, open('output.txt', 'w') as f2,\
                                                  open('temp_file','w') as f3:
    lines = []       # store lines between two `>` in this list
    for line in f1:
        if line.startswith('>'):
            if lines:
                f3.writelines(lines)
                lines = [line]
            else:
                lines.append(line)
        elif line.rstrip('\n') == 'Sequence unavailable':
            f2.writelines(lines + [line])
            lines = []
        else:
            lines.append(line)
    
    f3.writelines(lines)

os.remove('input.txt')
os.rename('temp_file', 'input.txt')

Demo:

$ cat input.txt
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket

$ python so.py

$ cat input.txt
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|EKB
Cricket
$ cat output.txt
>E8|E2|E10D
Sequence unavailable

For generating the temp file you can also use the tempfile module.

Method 2: fileinput module

No need of temp file with this method:

import fileinput
with open('output.txt', 'w') as f2:
    lines = []
    for line in fileinput.input('input.txt', inplace = True):
        if line.startswith('>'):
             if lines:
                 print "".join(lines),
                 lines = [line]
             else:
                 lines.append(line)
        elif line.rstrip('\n') == 'Sequence unavailable':
             f2.writelines(lines + [line])
             lines = []
        else:
             lines.append(line)

    with open('input.txt','a') as f:
        f.writelines(lines)


        
    
        
        
        
    
like image 53
Ashwini Chaudhary Avatar answered Mar 28 '26 07:03

Ashwini Chaudhary