简体   繁体   English

使用readlines()比创建List更好吗?

[英]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. 快速浏览一下,看起来我正在用我正在阅读的每一行覆盖变量,但是,它会创建一个列表,并且您可以使用type()方法验证contents是否为列表。

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. 在我快速浏览的地方,我创建了一个employee列表,我正在阅读每一行并附加到列表中。

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 ? 你看见with作为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. 我习惯了第一个和我一眼就看起来就像它是什么:调用readlines()来读取整个文件。

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 . 在我快速浏览的地方,我创建了一个员工列表,我正在阅读每一行并附加到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." 当我看到显式forwhile循环一次只能处理一个元素或一行时,我首先想到的是,“看起来像一个不习惯Python的C ++或Java程序员”。 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. 在Python中,通常使用批量操作,列表推导或生成器来避免旧的学校命令循环。

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. 作为一名资深的Python程序员,我更喜欢第一个。 If you want to read a file into memory you call readlines() . 如果要将文件读入内存,请调用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. 在这种情况下,惯用解决方案确实是for循环。 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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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