zoukankan      html  css  js  c++  java
  • python批量GBK转UTF-8

    有时候编码问题在导入工程的时候很烦,所以还是让世界都是"UTF-8"吧。

    抄来一段代码:

    #!/usr/env python
    # -*- coding: utf8 -*-
    import fnmatch
    import os
    import sys
    import codecs
    import chardet
    
    def find_files(path, fnexp="*"):
        for root, dirs, files in os.walk(path):
            for filename in fnmatch.filter(files, fnexp):
                yield os.path.join(root, filename)
    
    def ReadFile(filePath,encoding="gbk"):
        with codecs.open(filePath,"r",encoding) as f:
            return f.read()
     
    def WriteFile(filePath,u,encoding="utf-8"):
        with codecs.open(filePath,"w",encoding) as f:
            f.write(u)
     
    def GBK_2_UTF8(src,dst):
        content = ReadFile(src,encoding="gbk")
        WriteFile(dst,content,encoding="utf-8")
    
    def UTF8_2_GBK(src,dst):
        content = ReadFile(src,encoding="utf-8")
        WriteFile(dst,content,encoding="gbk")
    
    def trans(fpath):
        for fn in find_files(fpath):
            print fn
            d = chardet.detect(open(fn,"r").read())
            print d
            if d['encoding'] != 'utf-8':
                GBK_2_UTF8(fn,fn)
                print "ok"
    
    if __name__ == '__main__':
        if len(sys.argv) > 1 :
            for fpath in sys.argv[1:]:
                trans(fpath)
        else:
            fpath = raw_input("path:")
            trans(fpath)
        
    

      

  • 相关阅读:
    PowerDesigner中Table视图同时显示Code和Name
    sql语句 生成数据库表
    业务流程图
    物理模型图-数据库图
    观察者模式
    UML的九种图
    路由器工作原理
    web项目中处理捕获异常统一处理
    java中volatile、synchronized
    linux 安装软件的几种方法
  • 原文地址:https://www.cnblogs.com/fwindpeak/p/5051205.html
Copyright © 2011-2022 走看看