Sometimes when I use a for loop to iterate through lines of a file, I get each letter separately instead of each line. Can someone explain why that is?
Asked
Active
Viewed 2,055 times
1 Answers
0
See iter() and Iterator Types
for a in something:
pass
If something
s type is a itereable, a
will take all values of your iterable in turn. What exactly a
becomes is hidden inside the __iter__(self):
implementation of the object you are iterating over (afaik).
- If you iterate over a
list
, it will present you with all values in turns. - If you iterate over a
dict
, it will present you with alldict.keys()
in turns. - If you iterate over a
string
, it will present you each character in turn.
Generators and sequences also provide iterating over, see Understanding generators in Python and Sequence Types
If you iterate over a file, you will get (textfile, not binary) it line by line, unless you call other methods on the file first ():
Demodata:
with open("f.txt","w") as f:
f.write("""Some Text
Some text after a newline.
More text in a new line.""")
Iterate of file using it as iterable:
with open("f.txt","r") as r:
for c in r: # returns each single line including \n (thats the way next() of file works
print (c) # appends another \n due to default end='\n'
print("-"*30)
Output:
Some Text
Some text after a newline.
More text in a new line.
------------------------------
Iterate over file.read():
with open("f.txt","r") as r:
t = r.read() # returns a single string of the whole files content
for c in t: # you iterate characterwise over a string!
print (c)
print("-"*30)
Output:
S
o
m
e
[... snipped ...]
l
i
n
e
.
------------------------------
Iterate over file using readline():
with open("f.txt","r") as r:
for c in r.readline(): # returns only the first line as string and iterates over this string
print (c) # the other lines are omitted!
print("-"*30)
Output:
S
o
m
e
T
e
x
t
------------------------------
Iterate over file using readlines():
with open("f.txt","r") as r:
for c in r.readlines(): # returns a list of strings
print (c) # appends another \n due to default end='\n'
print("-"*30)
Output:
Some Text
Some text after a newline.
More text in a new line.
------------------------------
Iterage over binary file:
with open("f.bin","wb") as f:
f.write(b"294827523")
f.write(b"2927523")
f.write(b"-27523")
f.write(b"\n-27523")
with open("f.bin","rb") as r:
for b in r:
print (b)
Output:
b'2948275232927523-27523\n'
b'-27523'

Patrick Artner
- 50,409
- 9
- 43
- 69