过滤文本文件中包含特定单词的行

发布于 2024-10-21 04:03:29 字数 94 浏览 3 评论 0原文

我想编写一个程序,过滤文本文件中包含单词“apple”的行,并将这些行写入新的文本文件中。

我所尝试的只是在新文本文件中写入“apple”一词,而我想要整行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

茶色山野 2024-10-28 04:03:29

使用可以使用列表理解获取包含“apple”的所有行:

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

因此 - 同样在一个代码行中 - 您可以创建新的文本文件:

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

并且 eyquem 是对的:将其保留为迭代器并写入肯定会更快

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)
嗳卜坏 2024-10-28 04:03:29
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))
贵在坚持 2024-10-28 04:03:29

使用生成器,内存效率高且速度快,

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


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

apples = apple_finder(source)

我喜欢简单的解决方案,不会对阅读造成脑损伤:-)

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

明媚殇 2024-10-28 04:03:29

对于Python3 - 这是有效且快速的示例

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

测试:

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

有关代码的更多信息:

b'line contains text' - b< /code> 表示binary,我们对这种字符串进行操作,跳过一些编码等问题。
官方文档: https://docs.python.org /3/library/stdtypes.html?highlight=binary#bytes-objects

rb wb - 对 read进行操作>使用二进制类似对象进行写入操作
官方文档: https://docs.python.org/3/library /io.html#binary-io

filter() - 采用表达式可迭代对象。返回过滤后的对象。在我们的示例中,filter 获取所有行(可迭代对象),并为每行应用 lambda 通知 filter(如果给定) line 是否应该返回。

lambda - 包含两个元素参数表达式。在我们的示例中,lambda 检查line 是否包含给定的文本。检查表达式后返回TrueFalse

使用 lambdafilter 的示例:https://blog.finxter.com/how-to-filter-in-python-using-lambda-functions/

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/

趴在窗边数星星i 2024-10-28 04:03:29

if "apple" in line: 应该可以工作。

if "apple" in line: should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文