How to read a large file - line by line?

前端 未结 11 882
一整个雨季
一整个雨季 2020-11-21 11:44

I want to iterate over each line of an entire file. One way to do this is by reading the entire file, saving it to a list, then going over the line of interest. This method

11条回答
  •  既然无缘
    2020-11-21 12:15

    To strip newlines:

    with open(file_path, 'rU') as f:
        for line_terminated in f:
            line = line_terminated.rstrip('\n')
            ...
    

    With universal newline support all text file lines will seem to be terminated with '\n', whatever the terminators in the file, '\r', '\n', or '\r\n'.

    EDIT - To specify universal newline support:

    • Python 2 on Unix - open(file_path, mode='rU') - required [thanks @Dave]
    • Python 2 on Windows - open(file_path, mode='rU') - optional
    • Python 3 - open(file_path, newline=None) - optional

    The newline parameter is only supported in Python 3 and defaults to None. The mode parameter defaults to 'r' in all cases. The U is deprecated in Python 3. In Python 2 on Windows some other mechanism appears to translate \r\n to \n.

    Docs:

    • open() for Python 2
    • open() for Python 3

    To preserve native line terminators:

    with open(file_path, 'rb') as f:
        with line_native_terminated in f:
            ...
    

    Binary mode can still parse the file into lines with in. Each line will have whatever terminators it has in the file.

    Thanks to @katrielalex's answer, Python's open() doc, and iPython experiments.

提交回复
热议问题