如何用python删除文件尾行空行
1个回答
展开全部
Python读取一个文本文件,删除文本文件的空行代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def delblankline(infile, outfile):
""" Delete blanklines of infile """
infp = open(infile, "r")o
utfp = open(outfile, "w")
lines = infp.readlines()
for li in lines:
if li.split():
outfp.writelines(li)
infp.close()
outfp.close()
#调用示例
if
__name__ == "__main__":
delblankline("1.txt","2.txt")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def delblankline(infile, outfile):
""" Delete blanklines of infile """
infp = open(infile, "r")o
utfp = open(outfile, "w")
lines = infp.readlines()
for li in lines:
if li.split():
outfp.writelines(li)
infp.close()
outfp.close()
#调用示例
if
__name__ == "__main__":
delblankline("1.txt","2.txt")
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询