导语:
本文主要介绍了关于python怎么过滤掉空行?的相关知识,包括python敏感词过滤代码,以及python中eval函数作用这些编程知识,希望对大家有参考作用。
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、运行前后文件的对比:
有空行的文件:
运行代码生成无空行的文件:
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 如何定义python静态方法11/13
- ♥ 如何查看linux中安装的python版本10/18
- ♥ python处理列表的部分元素10/13
- ♥ python dict.item() 方法迭代字典11/11
- ♥ divmod 在 python 中接受复数形式01/11
- ♥ python操作xml的两种方法01/11
内容反馈