python實現批量將文件的編碼格式進行轉換


用python3實現批量將文件的編碼格式進行轉換;

需要指定四個參數,

1、搜索的根路徑

2、文件的類型(正則表達式來處理)

3、源編碼格式

4、目標編碼格式

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import codecs
import chardet
import re 

class TextDetect:
    path_root = ''
    file_pattern = ''

    def __init__(self, path_root, file_pattern):
        self.path_root = path_root
        self.file_pattern = file_pattern

    def print_member():
        print("path_root = %s"%(self.path_root))
        print("file_pattern = %s"%(self.file_pattern))


    def convert(self,file, in_enc="GBK", out_enc="UTF-8"):
        """
        該程序用於將目錄下的文件從指定格式轉換到指定格式,默認的是GBK轉到utf-8
        :param file:    文件路徑
        :param in_enc:  輸入文件格式
        :param out_enc: 輸出文件格式
        :return:
        """
        in_enc = in_enc.upper()
        out_enc = out_enc.upper()
        try:
            print("convert [ " + file.split('\\')[-1] + " ].....From " + in_enc + " --> " + out_enc)
            f = codecs.open(file, 'r', in_enc, "ignore")
            new_content = f.read()
            codecs.open(file, 'w', out_enc).write(new_content)
        except IOError as err:
            print("I/O error: {0}".format(err))


    def detect(self, in_enc="GBK", out_enc="UTF-8"):
        for root, dirs, files in os.walk(self.path_root, topdown=True):
            for item in files:
                match = re.match(self.file_pattern, item, re.IGNORECASE)
                if not match:
                    continue
                item_path = os.path.join(root,item)
                print("find file name = %s"%(item))
                with open(item_path, "rb") as f:
                    data = f.read()
                    codeType = chardet.detect(data)['encoding']
                    print("%s's codeType is %s"%(item, codeType))
                    if in_enc == "GBK": #GBK特殊處理一下
                        if codeType == 'GB2312' or codeType == 'GBK' or codeType == 'GB18030':
                            print("%s's codeType is %s,change encode!"%(item,codeType))
                            self.convert(item_path, codeType, out_enc)
                    else:
                        if codeType == in_enc:
                            print("%s's codeType is %s,change encode!"%(item,codeType))
                            self.convert(item_path, codeType, out_enc)



if __name__ == "__main__":
    # 使用時填寫這四個參數即可

    # 處理的根路徑
    path_root = "C:\\WorkSpace\\MyProjects\\python_basic_practice\\test"

    # 要進行格式轉換的文件正則表達式,匹配某一類型的文件
    search_file_pattern = ".*\.[ch]"

    # 要轉換的源編碼
    in_enc = "GBK"

    # 要轉換的目的編碼
    out_enc = "UTF-8"


    my_detect=TextDetect(path_root, search_file_pattern)
    my_detect.detect(in_enc, out_enc)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM