Python去除文本文件中的空行

功能

读取存在空行的文件,删除其中的空行,并将其保存到新的文件中;

代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/3/18 21:41
# @Author  : cunyu
# @Site    : cunyu1943.github.io
# @File    : deleteBlankLines.py
# @Software: PyCharm

"""
读取存在空行的文件,删除其中的空行,并将其保存到新的文件中
"""

with open('old.txt','r',encoding = 'utf-8') as fr,open('new.txt','w',encoding = 'utf-8') as fd:
        for text in fr.readlines():
                if text.split():
                        fd.write(text)
        print('输出成功....')
发布了104 篇原创文章 · 获赞 69 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/github_39655029/article/details/88692024