Comment vérifier EOF en Python? J'ai trouvé un bogue dans mon code où le dernier bloc de texte après le séparateur n'est pas ajouté à la liste de retour. Ou peut-être existe-t-il une meilleure façon d'exprimer cette fonction?Comment vérifier EOF en Python?
Voici mon 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()
return text_blocks
Essayez maintenant, fixe ma fonction x2 = ( –