Pregunta

f=open('C:\\Python33\\text file.txt','r')

for c in iter(lambda: f.read(1),'\n'):
    print(?)

How would I print out the values which lambda: f.read(1) yields please?

¿Fue útil?

Solución

Just print the c. That's what you're getting from iter() in your for loop.

f=open('C:\\Python33\\text file.txt','r')

for c in iter(lambda: f.read(1),'\n'):
    print(c)

Small improvement suggestion, use the with-statement:

with open('C:\\Python33\\text file.txt', 'r') as f:
    for c in iter(lambda: f.read(1), '\n'):
        print(c)

This way you won't have to call f.close().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top