2017-08-23 4 views
0

Mon codePEP234 iter, sortie écran inattendu

import sys 

f=open('iter.txt') 
for chunk in iter(lambda: f.read(20), ''): 
    n = sys.stdout.write(chunk) 
    print(n) 

écran

Iteration is one of 20 
Python’s strongest f20 
eatures. At a high l20 
evel, you might simp20 
ly view 
iteration as20 
a way to process it20 
ems in a sequence. H20 
owever, there is so 20 
much more that 
is po20 
ssible, such as crea20 
ting your own iterat20 
or objects, 
12 

Je pensais morceau à imprimer sans ce numérique value.How il vient qu'il apparaît? J'ai trouvé ce

iter(callable, sentinel) 
This form creates an iterator out of any callable, and will stop when an exception is raised or the sentinel value is returned by the callable. 

Quelle est la valeur sentinelle?

+1

Explicitement fait 'print (n)' - Wonders pourquoi il est fait. –

+0

J'utilise Python3. – MishaVacic

Répondre

1

sys.stdout.write fait la même chose que print sauf qu'il n'ajoute pas de nouvelle ligne à la fin. Donc, si vous voulez supprimer la valeur numérique, enlevez print(n).