Python 去除文本文件中的空行
发布日期:2021-05-08 06:30:55 浏览次数:18 分类:精选文章

本文共 471 字,大约阅读时间需要 1 分钟。

功能

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

代码

#!/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('输出成功....')
上一篇:Python 读取 Excel 中符合特定条件的数据,并写入新的表格
下一篇:爬取网易科技滚动新闻

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年03月26日 03时50分05秒