本文摘自php中文网,作者不言,侵删。
这篇文章主要介绍了python去掉空白行实现代码,需要的朋友可以参考下测试代码 php.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 1:www.php.cn
2:www.php.cn
3:www.php.cn
4:www.php.cn
5:www.php.cn
6:www.php.cn
7:www.php.cn
8:www.php.cn
9:www.php.cn
10:www.php.cn
11:www.php.cn
12:www.php.cn
13:www.php.cn
14:www.php.cn
15:www.php.cn
16:www.php.cn
|
python代码
代码一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # -*- coding: utf-8 -*-
'' '
python读取文件,将文件中的空白行去掉
'' '
def delblankline(infile, outfile):
infopen = open(infile, 'r' ,encoding= "utf-8" )
outfopen = open(outfile, 'w' ,encoding= "utf-8" )
lines = infopen.readlines()
for line in lines:
if line.split():
outfopen.writelines(line)
else :
outfopen.writelines( "" )
infopen.close()
outfopen.close()
delblankline( "php.txt" , "o.txt" )
|
代码二
阅读剩余部分
相关阅读 >>
Python是前端还是后端
简述Python如何调用系统底层api播放wav文件
Python新手入门之环境安装
Python中文件的读取和写入操作
Python中如何合并两个字典教程
Python如何使用列表
Python怎么统计不同字符的个数
Python怎么读写excel文件
Python学习笔记之open()函数打开文件路径报错问题
初学者编写Python用什么软件
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python去掉空白行的多种实现代码