Python脚本批处理JSON文件,去除制表符、空格、回车等多余符号

root_dir_path = "E:\popstar3\Popstar3_3.x_0_small\Resources\ui"  
extern_name_list = [".json", ".ExportJson"]  

import os  
import re  

def removeBlankChar(filepath):  
    f1 = open(filepath, "rb")  
    content1 = f1.read()  
    f1.close()  

    content2 = re.sub("\s", "", content1)  
    f2 = open(filepath, "wb")  
    f2.write(content2)  
    f2.close()  

    print "remove blank char complete. file: %s" % filepath  
# end of removeBlankChar  

def removeBlankCharOfDir(dirpath):  
    filename_list = os.listdir(dirpath)  
    for filename in filename_list:  
        filepath = dirpath + "/" + filename  
        if os.path.isdir(filepath):  
            removeBlankCharOfDir(filepath)  
        else:  
            for extern_name in extern_name_list:  
                if filepath[-len(extern_name):] == extern_name:  
                    removeBlankChar(filepath)  
                    break  
# end of removeBlankCharOfDir  

removeBlankCharOfDir(root_dir_path)  
raw_input("\n\ndone! ....")  

你可能感兴趣的:(个人笔记,python)