简体   繁体   English

Python中的问题,函数仅向文件写入一行

[英]Problem in Python with function only writing one line to file

I am trying to write a function in Python 3 that will write all lines that end with the string 'halloween' to a file. 我正在尝试在Python 3中编写一个函数,该函数会将所有以字符串“ halloween”结尾的行写入文件。 When I call this function, I can only get one line to write to the output file (file_2.txt). 调用此函数时,我只能写一行到输出文件(file_2.txt)。 Can anyone point out where my problem is? 谁能指出我的问题在哪里? Thanks in advance. 提前致谢。

def parser(reader_o, infile_object, outfile_object):
    for line in reader_o:
        if line.endswith('halloween'):
            return(line)

with open("file_1.txt", "r") as file_input:
    reader = file_input.readlines()
    with open("file_2.txt", "w") as file_output:
        file_output.write(parser(reader))
def parser(reader_o):
    for line in reader_o:
        if line.rstrip().endswith('halloween'):
            yield line

with open("file_1.txt", "r") as file_input:
    with open("file_2.txt", "w") as file_output:
        file_output.writelines(parser(file_input))

This is called a generator . 这称为发电机 It can also be written as an expression instead of a function: 也可以将其写为表达式而不是函数:

with open("file_1.txt", "r") as file_input:
    with open("file_2.txt", "w") as file_output:
        file_output.writelines(line for line in file_input if line.rstrip().endswith('halloween'))

If you're on Python 2.7 / 3.2, you can do the two with s like this: 如果您使用的是python 2.7 / 3.2,则可以with s来做到这两个:

with open("file_1.txt", "r") as file_input, open("file_2.txt", "w") as file_output:

You don't need to do readlines() on the file, just telling the loop to iterate over the open file itself will do the exact same thing. 您无需在文件上执行readlines() ,只需告诉循环对打开的文件本身进行迭代即可完成完全相同的操作。

Your problem was that return always would exit the loop on the first match. 您的问题是return总是会在第一个比赛中退出循环。 yield stops the loop, passes out the value, then the generator can be started again from the same point. yield停止循环,传递值,然后可以从同一点再次启动发电机。

line.endswith('halloween') might only work on the last of a file, since all other lines will have a newline appended. line.endswith('halloween')可能仅在文件的末尾起作用,因为所有其他行都将添加换行符。 rstrip the line first. rstrip Also, use yield instead of return . 另外,请使用yield而不是return

if line.rstrip().endswith('halloween'):
    yield line

Note that this will also strip off spaces at the end of the line, which may or may not be what you want. 请注意,这还将删除行尾的空格,可能不是您想要的。

You'll also have to modify your consumer to 您还必须将消费者修改为

with open("file_2.txt", "w") as file_output:
    for ln in parser(reader):
        file_output.write(ln)

Perhaps your parser function should be a generator. 也许您的解析器函数应该是生成器。 At the moment it's only called once and returns the first line that has "halloween" in it. 目前,它仅被调用一次,并返回其中包含“万圣节”的第一行。

Like the following: 如下所示:

def parser(reader_o):
    for line in reader_o:
        if line.endswith('halloween'):
            yield line

with open("file_1.txt", "r") as file_input:
    with open("file_2.txt", "w") as file_output:
        file_output.writelines(parser(file_input))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM