在 Python 中读取文件时忽略空行的最简单方法 [英] Easiest way to ignore blank lines when reading a file in Python

查看:216
本文介绍了在 Python 中读取文件时忽略空行的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以读取名称文件并创建一个列表:

I have some code that reads a file of names and creates a list:

names_list = open("names", "r").read().splitlines()

每个名称由换行符分隔,如下所示:

Each name is separated by a newline, like so:

Allman
Atkinson

Behlendorf 

我想忽略任何只包含空格的行.我知道我可以通过创建一个循环并检查我阅读的每一行然后将其添加到列表中(如果它不是空白的)来做到这一点.

I want to ignore any lines that contain only whitespace. I know I can do this by by creating a loop and checking each line I read and then adding it to a list if it's not blank.

我只是想知道是否有更 Pythonic 的方式来做这件事?

I was just wondering if there was a more Pythonic way of doing it?

推荐答案

我会堆叠生成器表达式:

I would stack generator expressions:

with open(filename) as f_in:
    lines = (line.rstrip() for line in f_in) # All lines including the blank ones
    lines = (line for line in lines if line) # Non-blank lines

现在,lines 是所有非空行.这将使您不必在线路上两次调用 strip.如果你想要一个行列表,那么你可以这样做:

Now, lines is all of the non-blank lines. This will save you from having to call strip on the line twice. If you want a list of lines, then you can just do:

with open(filename) as f_in:
    lines = (line.rstrip() for line in f_in) 
    lines = list(line for line in lines if line) # Non-blank lines in a list

你也可以用一行代码来完成(不包括 with 语句),但它并没有更高效和更难阅读:

You can also do it in a one-liner (exluding with statement) but it's no more efficient and harder to read:

with open(filename) as f_in:
    lines = list(line for line in (l.strip() for l in f_in) if line)

更新:

我同意这很丑,因为令牌的重复.如果你愿意,你可以只写一个生成器:

Update:

I agree that this is ugly because of the repetition of tokens. You could just write a generator if you prefer:

def nonblank_lines(f):
    for l in f:
        line = l.rstrip()
        if line:
            yield line

然后这样称呼它:

with open(filename) as f_in:
    for line in nonblank_lines(f_in):
        # Stuff

更新 2:

with open(filename) as f_in:
    lines = filter(None, (line.rstrip() for line in f_in))

在 CPython 上(使用确定性引用计数)

and on CPython (with deterministic reference counting)

lines = filter(None, (line.rstrip() for line in open(filename)))

在 Python 2 中,如果你想要一个生成器,请使用 itertools.ifilter,而在 Python 3 中,如果你想要一个列表,只需将整个内容传递给 list.

In Python 2 use itertools.ifilter if you want a generator and in Python 3, just pass the whole thing to list if you want a list.

这篇关于在 Python 中读取文件时忽略空行的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆