如何借助python删除文件中的所有空行? [英] How to delete all blank lines in the file with the help of python?

查看:72
本文介绍了如何借助python删除文件中的所有空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我们有这样的文件:

For example, we have some file like that:

第一行
第二行

第三行

first line
second line

third line

结果我们必须得到:

第一行
第二行
第三行

first line
second line
third line

只使用 python

推荐答案

import fileinput
for line in fileinput.FileInput("file",inplace=1):
    if line.rstrip():
        print line

这篇关于如何借助python删除文件中的所有空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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