简体   繁体   中英

Is using readlines() the better approach than creating a List?

I was reviewing some notes today and I came across this snippet of code:

def read_entire_file(file):
    with open(file) as f_obj:
        contents = f_obj.readlines()
    print(contents)
    print(type(contents)) # I added this line

A quick glance and it looks like I'm overwriting a variable with each line that I'm reading, however, it creates a list, and you can verify that contents is a list with the type() method.

Compare it to this code:

def read_file_into_list(file):

    employees = []

    with open(file) as f_obj:
       for line in f_obj:
           employees.append(line.strip())
    print(employees)

Where I can see at a quick glance, I created an employee list and I'm reading in each line and appending to the list.

I'm aware from this link that it reads the whole file into memory, but if you want to create a list from the data, which approach is better?

I like the second approach, while it's a little more code, it's clear what I'm doing, while the first approach, it isn't quite clear until you inspect it further.

A quick glance and it looks like I'm overwriting a variable with each line that I'm reading...

Why is that? Do you see the with as a while ? I am used to the first one and to me at a glance it looks like just what it is: a call to readlines() to read the entire file in.

Where I can see at a quick glance, I created an employee list and I'm reading in each line and appending to the List .

When I see explicit for and while loops that work one element or one line at a time, my first thought is, "Looks like a C++ or Java programmer who's not used to Python." It's a habit you should get out of. In Python there is oftentimes a more idiomatic approach using bulk operations, list comprehensions, or generators that avoids an old school imperative loop.

I like the second approach, while it's a little more code, it's clear what I'm doing, while the first approach, it isn't quite clear until you inspect it further.

As a veteran Python programmer, I prefer the first one. If you want to read a file into memory you call readlines() . It does what it says on the tin.

That said, I would also question whether reading the entire file into memory is the right thing to do. Perhaps you should process it line by line without chewing up a whole bunch of memory. In that case the idiomatic solution is indeed a for loop. But not one where you simply add all the elements to a list.

with open(file) as f_obj:
    for line in f_obj:
        process_line(line.strip())

Alternatively, if stripping the whitespace is important and you want to read everything into memory, consider a list comprehension.

with open(file) as f_obj:
    employees = [line.strip() for line in f_obj]

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