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)
        
    

      

  • 相关阅读:
    HUAS 1482 lsy的后宫(DP+矩阵快速幂)
    HUAS 1483 mex(离线+线段树)
    oracle 分页
    oracle 表查询(二)
    oracle 表查询(一)
    oracle 表的管理
    oracle 用户管理
    oracle sql*plus常用命令
    挑选数据库
    oracle sequence用法
  • 原文地址:https://www.cnblogs.com/fwindpeak/p/5051205.html
Copyright © 2011-2022 走看看