开发者

Filter lines from a text file which contain a particular word

开发者 https://www.devze.com 2023-02-15 14:17 出处:网络
I want to write a program which filters the lines from开发者_运维问答 my text file which contain the word \"apple\" and write those lines into a new text file.

I want to write a program which filters the lines from开发者_运维问答 my text file which contain the word "apple" and write those lines into a new text file.

What I have tried just writes the word "apple" in my new text file, whereas I want whole lines.


Use can get all lines containing 'apple' using a list-comprehension:

[ line for line in open('textfile') if 'apple' in line]

So - also in one code-line - you can create the new textfile:

open('newfile','w').writelines([ line for line in open('textfile') if 'apple' in line])

And eyquem is right: it's definitely faster to keep it as an iterator and write

open('newfile','w').writelines(line for line in open('textfile') if 'apple' in line)


from itertools import ifilter

with open('source.txt','rb') as f,open('new.txt','wb') as g:

    g.writelines( ifilter(lambda line: 'apple' in line, f))


Using generators, this is memory efficient and fast

def apple_finder(file):
    for line in file:
        if 'apple' in line:
             yield line


source = open('forest','rb')

apples = apple_finder(source)

I love easy solutions with no brain damage for reading :-)


For Python3 - here is working and fast example

    text = b'line contains text'
    with open('input.txt', 'rb') as file_in:
        with open('output.txt', 'wb') as file_out:
            file_out.writelines(
                filter(lambda line: text in line, file_in)
            )

Tests:

input.txt:

Test line contains text
Not line not contains this text

HEY
Another line contains text

output.txt:

Test line contains text
Another line contains text

More about code:

b'line contains text' - the b states for binary and we operating on this kind of string skipping some problems with encoding etc.
Official docs: https://docs.python.org/3/library/stdtypes.html?highlight=binary#bytes-objects

rb wb - operating on read and write operation with binary like objects
Official docs: https://docs.python.org/3/library/io.html#binary-i-o

filter() - takes expression and iterable object. Returns filtered object. In our example filter takes all lines (iterable object) and apply for each line lambda what inform filter if given line should be returned or not.

lambda - contains two elements argument: expression. In our example lambda check if line contains given text. Return True or False after expression check.

Example with lambda and filter: https://blog.finxter.com/how-to-filter-in-python-using-lambda-functions/


if "apple" in line: should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消