Andrea Moro
Andrea Moro

Reputation: 1031

Reading file in Python one line at a time

I do appreciate this question has been asked million of time, but I can't figure out while attempting to read a .txt file line by line I get the entire file read in one go.

This is my little snippet

    num = 0

with open(inStream, "r") as f:
    for line in f:
        num += 1
        print line + " ..."
        print num

Having a look at the open function there is anything that suggest a second param to limit the reading as that is just the "mode" to pen the file.

So I can only guess there are same problem with my file, but this is a txt file, with entry line by line.

Any hint?

Upvotes: 2

Views: 8921

Answers (1)

abarnert
abarnert

Reputation: 365607

Without a little more information, it's hard to be absolutely sure… but most likely, your problem is inappropriate line endings.


For example, on a modern Mac OS X system, lines in text files end with '\n' newline characters. So, when you do for line in f:, Python breaks the text file on '\n' characters.

But on classic Mac OS 9, lines in text files ended with '\r' instead. If you have some ancient classic Mac text files lying around, and you give one to Python, it will go looking for '\n' characters and not find any, so it'll think the whole file is one giant line.

(Of course in real life, Windows is a problem more often than classic Mac OS, but I used this example because it's simpler.)


Python 2: Fortunately, Python has a feature called "universal newlines". For full details, see the link, but the short version is that adding "U" onto the end of the mode when opening a text file means Python will read any of the three standard line-ending conventions (and give them to your code as Unix-style '\n').

In other words, just change one line:

with open(inStream, "rU") as f:

Python 3: Universal newlines are part of the standard behavior; adding "U" has no effect and is deprecated.

Upvotes: 4

Related Questions