返回 登录
0

python进行编码转换

完整源码如下:

import os,sys  

def convert( filename, in_enc = "UTF8", out_enc="GBK" ):  
    try:  
        print "convert " + filename,  
        content = open(filename).read()  
        new_content = content.decode(in_enc).encode(out_enc)  
        open(filename, 'w').write(new_content)  
        print " done"  
    except:  
        print " error"  

def explore(dir):  
    for root, dirs, files in os.walk(dir):  
        for file in files:  
            path = os.path.join(root, file)  
            convert(path)  

def main():  
    for path in sys.argv[1:]:  
        if os.path.isfile(path):  
            convert(path)  
        elif os.path.isdir(path):  
            explore(path)  

if __name__ == "__main__":  
    main() 

可参考文章:python进行编码转换

评论