在 Python 中删除带有 strip 的“\n”?

Remove "\n" with strip in Python?

我正在处理文件文本,但是,由于它的开头也有空格,当我尝试使用 strip 模式和 list 删除我的 \n 时理解,我得到一个包含空元素 (" ") 的列表,但我不知道如何删除它们。 我有一条短信,我的代码是:

with open(filename) as f:
    testo= f.readlines()
[e.strip() for e in testo]

但我得到这样的列表:

[' ', ' ', 'word1', 'word2', 'word3', ' ']

我想知道我是否可以使用 strip 方法解决它,否则使用其他方法。

您得到这些空字符串是因为有几行只是空换行符。这是清除这些空字符串的代码。

with open(filename) as f:
    testo = [e.strip() for e in f.readlines()]
    final_list = list(filter(lambda x: x != '', testo))
    print(final_list)

没有 lambda 并使用 map:

with open(filename) as f:
    final_list = list(filter(bool, map(str.strip, f)))
    print(final_list)

另一个解决方案是:

with open(filename) as f:
 testo =  [x for x in f.read().splitlines() if x]
 print(testo)

第二个解决方案的来源是:

性能升级参考@Patrick 的回答

您可以使用生成器读取所有行和 strip() 不需要的换行符。

在生成器中,您仅使用 "Truthy" 的元素 - 空字符串被视为 False

优点:您只创建一个列表并去掉空字符串:

写入文件:

filename = "t.txt"
with open(filename,"w") as f:
    f.write("""

  c
  oo
  l

  te
  xt
  """)

处理文件:

with open(filename) as f:
    testo = [x for x in (line.strip() for line in f) if x] # f.readlines() not needed. f is
                                                          # an iterable in its own right

print(testo)  # ['c', 'oo', 'l', 'te', 'xt']

你也可以这样做:

testo = [line.strip() for line in f if line.strip()]

但这会执行 strip() 两次,效率会稍低。

输出:

['c', 'oo', 'l', 'te', 'xt']

独库:


Eli Korvigo 的建议替代方案是:

testo = list(filter(bool, map(str.strip, f)))

with 本质上是相同的——使用生成器 comp 将显式列表 comp 替换为 f 上的 str.stripmap(生成生成器)并应用 filter 添加到列表中。

有关 filter,map,bool 的文档,请参阅 built in function

不过我更喜欢我的 ;o)

从您向我们展示的数据来看,其中一行似乎只有一个 space。考虑到这一点,您必须决定这是否是您想要的。

如果您需要,您的代码应如下所示:

with open(filename) as f:
   testo=f.readlines()
list(filter(None, (l.rstrip('\n') for l in testo)))

如果您不想要只有白色space 个字符的行,您可以这样做:

with open(filename) as f:
   testo=f.readlines()
[e.rstrip('\n') for e in testo if e.strip()]

在这种情况下,我们避免将:“带有前导和尾随 spaces 的单词”剥离为 "a word with leading and trailing spaces",因为在某些情况下它可能会改变行的语义:)