# coding = <a href="https://www.gaodaima.com/tag/utf" title="查看更多关于utf的文章" target="_blank">utf</a>-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 == ' ': line = line.strip(" ") file2.write(line) finally: file1.close() file2.close() if __name__ == '__main__': clearBlankLine()
www#gaodaima.com来源gaodai$ma#com搞$代*码网搞代码
2、运行前后文件的对比:
有空行的文件:
运行代码生成无空行的文件:
来源:搞代码网:原文地址:https://www.gaodaima.com