码迷,mamicode.com
首页 > 编程语言 > 详细

python批量GBK转UTF-8

时间:2015-12-16 15:23:32      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

有时候编码问题在导入工程的时候很烦,所以还是让世界都是"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)
    

  

python批量GBK转UTF-8

标签:

原文地址:http://www.cnblogs.com/fwindpeak/p/5051205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!