Python去除文件中的空格、Tab键和回车

 

def stripFile(oldFile, newFile):
    '''remove the space or Tab or enter in a file, and output a new file in the same folder'''
    f = open(oldFile, 'r+', encoding='utf8')
    newf = open(newFile, 'w',encoding='utf8')
    for eachline in f.readlines():
        newStr = eachline.replace(" ", "").replace("\t","").strip() 
        newf.write(newStr)
    f.close()
    newf.close()


if __name__ == "__main__":
    stripFile("lc_iocapp.postman_collection.json",'lc_iocapp.postman_collection_new.json')

 

去除文件中的空格、tab和回车,重新生成一个新文件。

 

如有更好的方法,请各位大虾不吝赐教!

 

posted @ 2019-06-19 17:48  宝山方圆  阅读(2692)  评论(0编辑  收藏  举报