Python: Filter lines from a text file which contain a particular word
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
add a comment |
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
add a comment |
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
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
python filter line
edited Mar 17 '11 at 3:17
Josh Caswell
59.2k12130176
59.2k12130176
asked Mar 9 '11 at 11:22
ahmadahmad
59113
59113
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
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)
4
A list comprehension creates an object. Using a generator expression would be better. By the way it can be writtenwritelines( 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 replaceif 'apple' in line
withif 'apple' in line or 'orange' in line
– phynfo
May 3 '17 at 20:24
add a comment |
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))
add a comment |
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 :-)
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
add a comment |
if "apple" in line:
should work.
add a comment |
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))
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
4
A list comprehension creates an object. Using a generator expression would be better. By the way it can be writtenwritelines( 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 replaceif 'apple' in line
withif 'apple' in line or 'orange' in line
– phynfo
May 3 '17 at 20:24
add a comment |
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)
4
A list comprehension creates an object. Using a generator expression would be better. By the way it can be writtenwritelines( 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 replaceif 'apple' in line
withif 'apple' in line or 'orange' in line
– phynfo
May 3 '17 at 20:24
add a comment |
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)
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)
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 writtenwritelines( 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 replaceif 'apple' in line
withif 'apple' in line or 'orange' in line
– phynfo
May 3 '17 at 20:24
add a comment |
4
A list comprehension creates an object. Using a generator expression would be better. By the way it can be writtenwritelines( 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 replaceif 'apple' in line
withif '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
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Mar 9 '11 at 11:58
eyquemeyquem
20.1k42739
20.1k42739
add a comment |
add a comment |
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 :-)
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
add a comment |
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 :-)
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
add a comment |
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 :-)
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 :-)
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
add a comment |
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
add a comment |
if "apple" in line:
should work.
add a comment |
if "apple" in line:
should work.
add a comment |
if "apple" in line:
should work.
if "apple" in line:
should work.
answered Mar 9 '11 at 11:37
neilneil
2,2311010
2,2311010
add a comment |
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 25 '18 at 17:34
pbaranskipbaranski
9,368116476
9,368116476
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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