python去掉空白行的多种实现代码


本文摘自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如何安装包vtk

Python判断闰年程序是什么

Python注释是什么

安装完Python怎么打开

Python字符串字母怎么取出

如何用Python画烟花

Python学哪个web框架好?2019 Python web框架排行

pycharm和Python区别

Python中什么是对象

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...