擅长:python、mysql、java
<p>这是发出缓冲区的标准问题。</p>
<p>你没有检测到EOF——那是不必要的。你写最后一个缓冲区。</p>
<pre><code>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()
### At this moment, you are at EOF
if len(text_block) > 0:
text_blocks.append( text_block.getvalue() )
### Now your final block (if any) is appended.
return text_blocks
</code></pre>