将指定文件夹下的文件转换成utf-8的

来源:互联网 发布:梦幻西游手游秒抢软件 编辑:程序博客网 时间:2024/04/28 15:34
tool_transform_to_utf8.py


#!/usr/bin/python# encoding:utf-8r'''遍历working/android/assets/下的csv文件,全部转换成utf-8格式'''import codecsimport osimport shutilimport sysfrom pip._vendor.requests.packages import chardetimport tool_envimport tool_file_utilimport tool_log_util__author__ = 'andrew'def list_all_files(root):    all_paths = tool_file_util.list_files_with_filter(root, ".csv", True)    return all_pathsdef to_utf8(file_paths):    hint = "to_utf8"    default_encode = "gb18030"    default_py_encode = "utf-8"    for src_path in file_paths:        csv_file = None        encoding_type = None        new_path = None        try:            csv_file = open(src_path, "r+")            old_text = csv_file.read()            code_inform = chardet.detect(old_text)            encoding_type = code_inform["encoding"]            if (encoding_type.lower() != "utf-8"):                window_encondings = ["gbk", "gb2312"]                lower = encoding_type.lower()                count = window_encondings.count(lower)                if (count > 0):                    tool_log_util.verb(                        "windows encoding. src_path = %s,  encoding_type = %s" % (src_path, encoding_type))                    encoding_type = default_encode                tool_log_util.verb("src_path = %s,  encoding_type = %s" % (src_path, encoding_type))                parent = os.sep.join(src_path.split(os.sep)[0:-1])                parent_path=os.sep.join((parent,"logs"))                new_path = os.sep.join((parent_path, "temp_new_path.txt"))                try:                    os.makedirs(parent_path)                except:                    pass                new_text = unicode(old_text, encoding_type)                new_file = codecs.open(new_path, "w+", default_py_encode)                new_file.write(new_text)                new_file.close()                shutil.copy(new_path, src_path)                os.remove(new_path)        except ValueError, e:            tool_log_util.error(                    "ValueError src_path={0}, encoding_type = {1}, e = {2}".format(src_path, encoding_type, e))        except Exception, e:            tool_log_util.error(                    "Exception src_path={0}, encoding_type = {1}, e = {2}".format(src_path, encoding_type, e))        finally:            if csv_file != None:                csv_file.close()            if new_path != None:                shutil.rmtree(new_path, True)                new_path = Noneif __name__ == "__main__":    names = sys.argv    file_paths = []    if len(names) > 1:        print("len argv =%d" % (len(names)))        tool_log_util.verb("Lu debug if 1")        for path in names[1:]:            if os.path.exists(path):                if os.path.isfile(path):                    file_paths.append(path)                elif os.path.isdir(path):                    files = list_all_files(path)                    file_paths.extend(files)                        else:        # woking_files = list_all_files(tool_env.g_working_root)        # file_paths.extend(woking_files)        working_assets_files = list_all_files(tool_env.g_working_assets)        file_paths.extend(working_assets_files)    to_utf8(file_paths)

tool_env.py
#!/usr/bin/python# encoding:utf-8import inspectimport osdef get_root_path():    script_path=inspect.getfile(inspect.currentframe())    root=os.path.dirname(script_path)    return  rootcur=get_root_path()g_xcf_root=os.sep.join((cur,"..","dy_cike_xcf"))g_xcf_assets=os.sep.join((g_xcf_root,"assets"))g_xcf_config=os.sep.join((g_xcf_assets,"config"))g_xcf_cocos=os.sep.join((g_xcf_assets,"cocostudio"))g_xcf_skeleton=os.sep.join((g_xcf_assets,"skeleton"))g_xcf_font=os.sep.join((g_xcf_assets,"font"))g_working_root=get_root_path()g_working_assets=os.sep.join((cur, "android", "assets"))g_working_config=os.sep.join((g_working_assets, "config"))g_working_skeleton=os.sep.join((g_working_assets, "skeleton"))g_working_font=os.sep.join((g_working_assets, "font"))g_working_cocos=os.sep.join((g_working_assets, "cocostudio"))g_working_atlas=os.sep.join((g_working_assets, "atlas"))g_text_file_suffixs = (".csv", ".json")g_file_forbid_words = os.sep.join((g_xcf_config, "forbid_words.txt"))g_file_used_words = os.sep.join((g_xcf_config, "cike_used_words.txt"))g_default_encode = "gb18030"g_default_py_encode = "utf-8"



tool_file_util.py
#!/usr/bin/python# encoding:utf-8r''' 处理文件'''import osimport tool_env__author__ = 'andrew'## 列出所有文件(不包括文件夹)def list_files_with_filter(root, suffix=None, recursion=False):    hint = "list_files_with_filter"    children = []    os_listdir = os.listdir(root)    for f in os_listdir:        path = os.sep.join((root, f))        if (os.path.isdir(path)):            if (recursion):                grandSons = list_files_with_filter(path, suffix, recursion)                children.extend(grandSons)        else:            if (suffix == None or path.endswith(suffix)):                children.append(path)    return children    # os_listdir = os.listdir(root)    # for f in os_listdir:    #     if (f.endswith(suffix)) or (suffix == None):    #         path = os.sep.join((root, f))    #         yield pathif __name__ == "__main__":    children = list_files_with_filter(tool_env.g_xcf_assets, ".csv",True)    count = 0    for f in children:        count += 1        print("file count = {0}, f={1}".format(count, f))


tool_log_util.py
#!/usr/bin/python# encoding:utf-8import sysdef succes(inform):    print("%s %s  %s" % ('\033[1;32;40m :', inform, "\033[0m"))def verb(inform):    print("%s %s  %s" % ("\033[0m", inform, "\033[0m"))def error(error_inform):    print("%s %s  %s" % ('\033[1;33;40m :', error_inform, "\033[0m"))    sys.exit(1)




0 0
原创粉丝点击