将指定文件夹下的文件转换成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
- 将指定文件夹下的文件转换成utf-8的
- 将jbk格式的文件转换成utf-8的
- 将指定文件夹下的所有单个文件 打包
- 将指定文件夹下的所有文件打包下载
- 将指定文件夹下的文件copy到指定的文件夹
- 将指定文件夹及子文件夹下的指定文件移动
- 用批处理 将文件夹下的所有指定后缀名的文件,修改成自定义后缀名
- C++ 将指定文件夹下的所有文件拷贝到另一文件夹下
- java-将任意文件夹下的某一类型的文件复制到指定的文件夹
- 将指定文件夹下文件及子文件夹下的文件复制到同一级目录
- 怎样将utf-8格式的文件转换成gbk格式的文件
- Python:将utf-8格式的文件转换成gbk格式的文件
- java 将某一文件夹下的所有文件 复制到指定文件下
- Android将需要的日志文件LOG记录到本地文件夹下指定的文件
- 将文件夹下面的文件移到指定文件夹
- android:将assets目录下的文件(文件夹)放置到内存卡指定目录下
- 获得指定文件夹下的文件列表
- 查找指定文件夹下的文件
- Android自定义View之高仿卫星发散控件
- leetcode First Unique Character in a String
- app加载广告
- 图标字体详细制作过程介绍
- shell 控制台运行lua代码
- 将指定文件夹下的文件转换成utf-8的
- 线程协作(生产者与消费者问题)
- 微信支付、退款配置笔记
- js中的类型判断typeof、instanceof、constructor和prototype
- React-Native 与原生的3种交互通信(Android)
- AJAX表单提交以及数据接收
- Python入门——条件判断与循环
- 基于K8s和docker的Jenkins 可伸缩持续集成系统
- Linux系统备份与恢复