iterating over the lines of a file - difference between Python 2.7 and 3?

Peter Otten __peter__ at web.de
Thu Jan 17 12:59:46 EST 2013


Wolfgang Maier wrote:

> What will my IO object return then when I read from it in Python 2.7? str
> where Python3 gives bytes, and unicode instead of str ? This is what I
> understood from the Python 2.7 io module doc.

You can always double-check in the interpreter:
 
>>> with open("tmp.txt", "w") as f: f.write("let's try\n")
... 
>>> import io
>>> with io.open("tmp.txt", "r") as f: print type(f.read())
... 
<type 'unicode'>
>>> with io.open("tmp.txt", "rb") as f: print type(f.read())
... 
<type 'str'>

So yes. Also:

>>> str is bytes
True





More information about the Python-list mailing list