https://blog.csdn.net/xiaozhi0999/article/details/52469979
上文提到了ue4中出现中文乱码问题,可通过修改文件编码格式解决,但当文件很多的时候,一个个修改文件编码比较费事,所以本人用python写了一个批量转换编码工具(gb2312转utf8),其他编码转换,只需要对代码稍作修改即可。
python2.7
import os
def fileGb2312ToUtf8(filename):
with open(filename, "r") as binary_file:
data = binary_file.read()
with open(filename, "wb") as binary_file:
buff = b'\xef\xbb\xbf'
binary_file.write(buff)
with open(filename, "ab") as binary_file:
try:
result = data.decode("gb2312").encode("utf-8")
binary_file.write(result)
except Exception as err:
print filename
print err
def fileExtension(filename):
return os.path.splitext(filename)[1]
def isCodeFile(filename):
extension = fileExtension(filename)
return (cmp(extension, '.h') == 0) or (cmp(extension, '.cpp') == 0) or (cmp(extension, '.cs') == 0)
def isUtf8File(filename):
with open(filename, "rb") as binary_file:
binary_file.seek(0)
data = bytearray(binary_file.read(3))
return data[0] == 0xef and data[1] == 0xbb and data[2] == 0xbf
return False
def dirGb2312ToUtf8(dir):
list = []
for path,dirs,fs in os.walk(dir):
for f in fs:
fullPath = os.path.join(path,f)
list.append(fullPath)
for i, filename in enumerate(list):
if (isUtf8File(filename)):
print filename + ' encode is utf8 already!'
continue
if (isCodeFile(filename)):
fileGb2312ToUtf8(filename)
dirGb2312ToUtf8('F:\work\MyProject\Source\MyProject')
python3.6
import os
def fileGb2312ToUtf8(filename):
with open(filename, "r") as binary_file:
data = binary_file.read()
with open(filename, "wb") as binary_file:
buff = b'\xef\xbb\xbf'
binary_file.write(buff)
with open(filename, "ab") as binary_file:
try:
result = data.encode("utf-8")
binary_file.write(result)
except Exception as err:
print(filename)
print(err)
def fileExtension(filename):
return os.path.splitext(filename)[1]
def cmp(a, b):
return (a > b) - (a < b)
def isCodeFile(filename):
extension = fileExtension(filename)
return (cmp(extension, '.h') == 0) or (cmp(extension, '.cpp') == 0) or (cmp(extension, '.cs') == 0)
def isUtf8File(filename):
with open(filename, "rb") as binary_file:
binary_file.seek(0)
data = bytearray(binary_file.read(3))
return data[0] == 0xef and data[1] == 0xbb and data[2] == 0xbf
return False
def dirGb2312ToUtf8(dir):
list = []
for path,dirs,fs in os.walk(dir):
for f in fs:
fullPath = os.path.join(path,f)
list.append(fullPath)
for i, filename in enumerate(list):
if (isUtf8File(filename)):
print(filename + ' encode is utf8 already!')
continue
if (isCodeFile(filename)):
fileGb2312ToUtf8(filename)
dirGb2312ToUtf8('F:\work\DeathFire\Source\DeathFire')
python文件下载及使用说明:
https://download.csdn.net/download/xiaozhi0999/10495232