新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么過濾掉空行?
1、python過濾掉空行的代碼:

# coding = utf-8
def clearBlankLine():
file1 = open('text1.txt', 'r', encoding='utf-8') # 要去掉空行的文件
file2 = open('text2.txt', 'w', encoding='utf-8') # 生成沒有空行的文件
try:
for line in file1.readlines():
if line == '\n':
line = line.strip("\n")
file2.write(line)
finally:
file1.close()
file2.close()
if __name__ == '__main__':
clearBlankLine()2、運行前后文件的對比:
有空行的文件:
運行代碼生成無空行的文件:
分享標題:創(chuàng)新互聯(lián)Python教程:python怎么過濾掉空行?
分享網(wǎng)址:http://m.fisionsoft.com.cn/article/dhdohdd.html


咨詢
建站咨詢
