user3925736 user3925736 - 4 months ago 19
Python Question

filtering out some lines

I have a text file (a.txt). here a small part of that:

ENSG00000060642.6 0,023999998 0,015999999 0,666666667 0,006410256 0,006410256 1,000000073 0,016393442 0,016393442 1 0,020202022 0,030303031 1,499999908
ENSG00000149136.3 0,03508772 0,01754386 0,5 0,068627447 0,029411765 0,428571456 0,078947365 0,065789476 0,833333396 0,066666663 0,066666663 1
ENSG00000104889.4 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0!
ENSG00000157827.15 0,055555556 0,037037037 0,666666667 0,032258064 0,048387095 1,5 0,150000006 0,024999999 0,16666665 0,222222224 0,037037037 0,166666667
ENSG00000146067.11 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0!
ENSG00000167700.4 0,299999982 0 0 0,071428567 0,071428567 1 0 0 #DIV/0! 0 0 #DIV/0!
ENSG00000172137.14 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0! 1 0 0
ENSG00000178776.4 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0! 0 0 #DIV/0!


I want to filter out all lines including "#DIV/0!", even if there is only one "#DIV/0!", and make a new text file.

Answer

You could do it this way which is incremental (so it doesn't read the entire file into memory at one time):

from itertools import ifilter

with open('a.txt', 'r') as inf, open('new.txt', 'w') as outf:
    outf.writelines(ifilter(lambda line: '#DIV/0!' not in line, inf))