简体   繁体   中英

readlines not reading the last line of the file in python

I have a code where I am reading all the lines from the file using readlines function and I am further parsing each line in a list. But when I printed the list I saw that the loop is ignoring the last line in the file. When I inserted a blank line in the file then all the contents are read. can you pls tell me why it is doing that

def readFile1(file1):
    f = file1.readlines()
    cList1 = []
    for line in f:
        if re.findall('\n',line):
            v = re.sub('\n','',line)
        cList1.append(v)

    print cList1

This is printing all the contents except the last line of the file.

If the last line doesn't end with a newline, your code won't add it to cList1 . Instead, it would add a second copy of the penultimate line (which is still stored in v ).

A cleaner way to write that loop is:

cList1 = []
for line in f:
    cList1.append(line.rstrip('\n'))

Or, indeed:

cList1 = [line.rstrip('\n') for line in f]

In fact, I would avoid the readlines() call entirely:

def readFile1(file1):
    cList1 = [line.rstrip('\n') for line in file1]
    print cList1

If you just want to get all lines from a file into a list, there's a much easier (and cleaner, in my opinion) way.

def readFile1(file1):
    cList1 = file1.read().splitlines()
    print cList1

I don't think there's any need to use a generator in this case. Also, I benchmarked it (on Windows) and the generator form that @aix gave is slightly slower in some cases .

>>> import timeit
>>> import os
>>>
>>> # Setup
>>> open('testfile', 'w').write('This Is A Test' * 500)
>>>
>>> # Time generator form (ten thousand times)
>>> timeit.timeit("lst = [line.rstrip('\\n') for line in open('testfile')]", 
...     number=10000)
2.656837282256163
>>>
>>> # Time splitlines() form (ten thousand times)
>>> timeit.timeit("lst = open('testfile').read().splitlines()", number=10000)
1.3464799954204238
>>>
>>> # Cleanup
>>> os.remove('testfile')

您的最后一行没有\\ n字符,因为您之后没有新行。

print f actually prints all lines. It's a bug in your code. You append the second-to-last line twice, since the last line does not contain \\n . You're missing eg an else block that assign v when it doesn't contain a \\n .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM