Je lis dans les lignes en utilisant le code suivant:Arrêt Lorsque EOF On Line Python
for line in sys.stdin:
print('Output: ', end='')
print(line.rstrip('\n'))
Comme je tape je vois sortie tels que:
test
Output: test
another test
Output: another test
eof at end of line^DOutput: eof at end of line
I can still type here
Output: I can still type here
...
Dans ce cas ^D
signifie en tapant CTRL- D ou le caractère EOF.
Mon programme doit se terminer lorsqu'un EOF se produit à la fin d'une ligne, et pas seulement lorsqu'il s'agit du seul caractère d'une ligne. Comment puis-je obtenir ce comportement?
comportement Exemple:
test
Output: test
another test
Output: another test
eof at end of line^DOutput: eof at end of line
[program terminates]
Le pilote de terminal ne ferme pas le flux et interprète toujours '^ D'. Mais il fait juste 'read (2)' return, ce qui implique seulement EOF quand il retourne 0. –
Y at-il un moyen d'obtenir le comportement que je veux? Je sais en C je peux utiliser quelque chose comme 'while (getline (& line, & n, stdin)! = -1)' et cela va produire mon comportement attendu. – carloabelli