1. 程式人生 > >Python指令碼批處理JSON檔案,去除製表符、空格、回車等多餘符號

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! ....")