如何在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