1. 程式人生 > >去除檔案中的空行

去除檔案中的空行

def clear_space():
    with open("test","r",encoding="utf-8") as fr:
        for line in fr:
            line = line.strip()
            if len(line) > 0:
                yield line
g = clear_space()
for line in g:
    with open("test.bak","a",encoding="utf-8") as fw:
        fw.write(line+"\n")