0%

20240625老的 asp.net 项目中文件 gb2312 编码统一

传统windows下面的文件,很多老的项目中文件编码有gb2312的,也有unicode的,在vscode中修改的话经常会出错,因为写了个小脚本,把所有非unicode编码的文件统一改为unicode编码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding:utf-8 -*-
__author__ = 'whx'

import os,sys
import chardet
import glob

def convert( filename, in_enc = "GBK", out_enc="UTF-8-SIG" ):
try:
content = open(filename, 'rb').read()
result = chardet.detect(content)#通过chardet.detect获取当前文件的编码格式串,返回类型为字典类型
coding = result.get('encoding')#获取encoding的值[编码格式]
if "UTF" not in coding and "utf" not in coding :#文件格式如果不是utf-8的时候,才进行转码
print ("转换 "+ coding + "to utf-8! " + filename)
new_content = content.decode( coding ).encode(out_enc)
open(filename, 'wb').write( new_content )
print (" done")
else:
print ("不用修改 "+coding + ":" +filename)
except Exception as ex:

print (" error",ex)



filesAspx = glob.glob("D:\source\project/**/*.aspx",recursive=True)
filesCs = glob.glob("D:\source\project/**/*.cs",recursive=True)

for file_path in filesAspx:
convert(file_path)

for file_path in filesAspx:
convert(filesCs)