如何在Python中检查EOF?

aju*_*shi 8 python eof

如何在Python中检查EOF?我在代码中发现了一个错误,其中分隔符后的最后一个文本块未添加到返回列表中.或者也许有更好的方式来表达这个功能?

这是我的代码:

def get_text_blocks(filename):
    text_blocks = []
    text_block = StringIO.StringIO()
    with open(filename, 'r') as f:
        for line in f:
            text_block.write(line)
            print line
            if line.startswith('-- -'):
                text_blocks.append(text_block.getvalue())
                text_block.close()
                text_block = StringIO.StringIO()
    return text_blocks
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 2

您可能会发现使用itertools.groupby更容易解决此问题。

def get_text_blocks(filename):
    import itertools
    with open(filename,'r') as f:
        groups = itertools.groupby(f, lambda line:line.startswith('-- -'))
        return [''.join(lines) for is_separator, lines in groups if not is_separator]
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用正则表达式来匹配分隔符:

def get_text_blocks(filename):
    import re
    seperator = re.compile('^-- -.*', re.M)
    with open(filename,'r') as f:
        return re.split(seperator, f.read())
Run Code Online (Sandbox Code Playgroud)