Python: Filter lines from a text file which contain a particular word












11















In Python, 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. I am a beginner in Python, so kindly reply to my question, as I really need this.










share|improve this question





























    11















    In Python, 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. I am a beginner in Python, so kindly reply to my question, as I really need this.










    share|improve this question



























      11












      11








      11


      7






      In Python, 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. I am a beginner in Python, so kindly reply to my question, as I really need this.










      share|improve this question
















      In Python, 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. I am a beginner in Python, so kindly reply to my question, as I really need this.







      python filter line






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 17 '11 at 3:17









      Josh Caswell

      59.2k12130176




      59.2k12130176










      asked Mar 9 '11 at 11:22









      ahmadahmad

      59113




      59113
























          5 Answers
          5






          active

          oldest

          votes


















          20














          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)





          share|improve this answer





















          • 4





            A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

            – eyquem
            Mar 9 '11 at 12:03













          • @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

            – phynfo
            Mar 9 '11 at 12:23













          • @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

            – Chris Cogdon
            Nov 4 '15 at 0:46











          • Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

            – Gajendra D Ambi
            Apr 26 '17 at 8:13











          • You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

            – phynfo
            May 3 '17 at 20:24



















          10














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





          share|improve this answer































            7














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






            share|improve this answer



















            • 1





              The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

              – eyquem
              Mar 9 '11 at 12:39





















            1














            if "apple" in line: should work.






            share|improve this answer































              0














              For Python3 - here is working and fast example



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





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5245058%2fpython-filter-lines-from-a-text-file-which-contain-a-particular-word%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                20














                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)





                share|improve this answer





















                • 4





                  A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

                  – eyquem
                  Mar 9 '11 at 12:03













                • @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

                  – phynfo
                  Mar 9 '11 at 12:23













                • @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

                  – Chris Cogdon
                  Nov 4 '15 at 0:46











                • Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

                  – Gajendra D Ambi
                  Apr 26 '17 at 8:13











                • You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

                  – phynfo
                  May 3 '17 at 20:24
















                20














                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)





                share|improve this answer





















                • 4





                  A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

                  – eyquem
                  Mar 9 '11 at 12:03













                • @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

                  – phynfo
                  Mar 9 '11 at 12:23













                • @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

                  – Chris Cogdon
                  Nov 4 '15 at 0:46











                • Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

                  – Gajendra D Ambi
                  Apr 26 '17 at 8:13











                • You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

                  – phynfo
                  May 3 '17 at 20:24














                20












                20








                20







                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)





                share|improve this answer















                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)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 3 '17 at 20:27

























                answered Mar 9 '11 at 11:37









                phynfophynfo

                3,06211436




                3,06211436








                • 4





                  A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

                  – eyquem
                  Mar 9 '11 at 12:03













                • @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

                  – phynfo
                  Mar 9 '11 at 12:23













                • @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

                  – Chris Cogdon
                  Nov 4 '15 at 0:46











                • Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

                  – Gajendra D Ambi
                  Apr 26 '17 at 8:13











                • You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

                  – phynfo
                  May 3 '17 at 20:24














                • 4





                  A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

                  – eyquem
                  Mar 9 '11 at 12:03













                • @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

                  – phynfo
                  Mar 9 '11 at 12:23













                • @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

                  – Chris Cogdon
                  Nov 4 '15 at 0:46











                • Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

                  – Gajendra D Ambi
                  Apr 26 '17 at 8:13











                • You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

                  – phynfo
                  May 3 '17 at 20:24








                4




                4





                A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

                – eyquem
                Mar 9 '11 at 12:03







                A list comprehension creates an object. Using a generator expression would be better. By the way it can be written writelines( line for line in open('textfile') if 'apple' in line)

                – eyquem
                Mar 9 '11 at 12:03















                @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

                – phynfo
                Mar 9 '11 at 12:23







                @eyquem: Ok, I totally agree that -- for large files -- it should be the better to use generators, since a generator behaves lazily and thus doesnt consume that much memory. But probably for small files the list-comprehension is the faster solution?

                – phynfo
                Mar 9 '11 at 12:23















                @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

                – Chris Cogdon
                Nov 4 '15 at 0:46





                @Phynfo: Nope... keeping things as generators/iterators is far more efficient. The list comprehension is still creating the iterator, which is then filling a list, and once complete passing that list to writelines which turns it back into an iterator.

                – Chris Cogdon
                Nov 4 '15 at 0:46













                Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

                – Gajendra D Ambi
                Apr 26 '17 at 8:13





                Can I use multiple strings here to match? Ex: I want to retain only line with string 'apple' or 'orange'

                – Gajendra D Ambi
                Apr 26 '17 at 8:13













                You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

                – phynfo
                May 3 '17 at 20:24





                You can replace if 'apple' in line with if 'apple' in line or 'orange' in line

                – phynfo
                May 3 '17 at 20:24













                10














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





                share|improve this answer




























                  10














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





                  share|improve this answer


























                    10












                    10








                    10







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





                    share|improve this answer













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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 9 '11 at 11:58









                    eyquemeyquem

                    20.1k42739




                    20.1k42739























                        7














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






                        share|improve this answer



















                        • 1





                          The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

                          – eyquem
                          Mar 9 '11 at 12:39


















                        7














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






                        share|improve this answer



















                        • 1





                          The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

                          – eyquem
                          Mar 9 '11 at 12:39
















                        7












                        7








                        7







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






                        share|improve this answer













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







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 9 '11 at 12:07









                        Mario CésarMario César

                        2,25621937




                        2,25621937








                        • 1





                          The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

                          – eyquem
                          Mar 9 '11 at 12:39
















                        • 1





                          The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

                          – eyquem
                          Mar 9 '11 at 12:39










                        1




                        1





                        The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

                        – eyquem
                        Mar 9 '11 at 12:39







                        The function apple_finder(file) is a function generator and apples is a generator. The latter do the same job as ifilter(lambda line: 'apple' in line, f) in two lines (import comprised)

                        – eyquem
                        Mar 9 '11 at 12:39













                        1














                        if "apple" in line: should work.






                        share|improve this answer




























                          1














                          if "apple" in line: should work.






                          share|improve this answer


























                            1












                            1








                            1







                            if "apple" in line: should work.






                            share|improve this answer













                            if "apple" in line: should work.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 9 '11 at 11:37









                            neilneil

                            2,2311010




                            2,2311010























                                0














                                For Python3 - here is working and fast example



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





                                share|improve this answer




























                                  0














                                  For Python3 - here is working and fast example



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





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    For Python3 - here is working and fast example



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





                                    share|improve this answer













                                    For Python3 - here is working and fast example



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






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 25 '18 at 17:34









                                    pbaranskipbaranski

                                    9,368116476




                                    9,368116476






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5245058%2fpython-filter-lines-from-a-text-file-which-contain-a-particular-word%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        404 Error Contact Form 7 ajax form submitting

                                        How to know if a Active Directory user can login interactively

                                        TypeError: fit_transform() missing 1 required positional argument: 'X'