What makes parsing a text file in ‘r’ mode more convenient than parsing it in ‘rb’ mode?
Especially when the text file in question may contain non-ASCII characters.
What makes parsing a text file in ‘r’ mode more convenient than parsing it
Share
This depends a little bit on what version of Python you’re using. In Python 2, Chris Drappier’s answer applies.
In Python 3, its a different (and more consistent) story: in text mode (
'r'), Python will parse the file according to the text encoding you give it (or, if you don’t give one, a platform-dependent default), andread()will give you astr. In binary ('rb') mode, Python does not assume that the file contains things that can reasonably be parsed as characters, andread()gives you abytesobject.Also, in Python 3, the universal newlines (the translating between
'\n'and platform-specific newline conventions so you don’t have to care about them) is available for text-mode files on any platform, not just Windows.