python-删除空格

# coding = utf-8 
# Python读取一个文本文件,删除文本文件的空行代码如下:   
def delblankline(infile, outfile):
    """ Delete blanklines of infile """
    infp = open(infile, "r", encoding='utf-8') # 要去掉空行的文件
    outfp = open(outfile, "w", encoding='utf-8') # 生成没有空行的文件
    try:
        lines = infp.readlines()
        for li in lines:
            if li.split():
                outfp.writelines(li)
    finally:
        infp.close()
        outfp.close()

# 调用示例
if __name__ == '__main__':
    delblankline("data.txt", "data1.txt")

 

你可能感兴趣的:(python)