【问题标题】:Remove all blank lines from a text file in python从python中的文本文件中删除所有空行
【发布时间】:2021-05-27 08:58:44
【问题描述】:

我试图弄清楚如何在仅使用 python 时从文本文件中删除一个空行。 输入应该是这样的:

firstline
secondline

thirdline

输出应该是:

firstline
secondline
thirdline

所以我现在有这个......

import sys

with open("New Text Document.txt") as f:
    for line in f:
        if not line.isspace():
            sys.stdout.write(line)

【问题讨论】:

标签: python txt


【解决方案1】:
file = open('demo.txt', 'r')
arr = file.readlines()
print([v for v in arr if not v.isspace()])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2021-11-22
    • 2014-08-10
    相关资源
    最近更新 更多