Survivalcraft API 1.8.2.3
v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
convert_to_utf8.py
浏览该文件的文档.
1
import
os
2
import
chardet
3
4
def
convert_to_utf8
(file_path):
5
"""检测并转换文件编码为 UTF-8"""
6
# 读取文件的原始二进制数据
7
with
open(file_path,
'rb'
)
as
f:
8
raw_data = f.read()
9
result = chardet.detect(raw_data)
10
encoding = result[
'encoding'
]
11
12
# 跳过已经是 UTF-8 的文件
13
if
encoding
and
encoding.lower()
in
[
'utf-8'
,
'ascii'
]:
14
print(f
"[✓] Skipped {file_path} (encoding: {encoding})"
)
15
return
16
17
# 其他编码的文件转换为 UTF-8
18
try
:
19
with
open(file_path,
'r'
, encoding=encoding, errors=
'ignore'
)
as
f:
20
content = f.read()
21
with
open(file_path,
'w'
, encoding=
'utf-8'
, newline=
'\n'
)
as
f:
22
f.write(content)
23
print(f
"[🔥] Converted {file_path} from {encoding} to UTF-8"
)
24
except
Exception
as
e:
25
print(f
"[⚠] Failed to convert {file_path} (encoding: {encoding}) - {e}"
)
26
27
def
process_directory
(root_dir):
28
"""遍历项目中的所有 .cs 文件"""
29
for
root, _, files
in
os.walk(root_dir):
30
for
file
in
files:
31
if
file.endswith(
'.cs'
):
32
convert_to_utf8
(os.path.join(root, file))
33
34
if
__name__ ==
"__main__"
:
35
project_path =
"."
# 当前目录
36
process_directory
(project_path)
37
print(
"\n✅ 所有非 UTF-8 文件已转换为 UTF-8!"
)
convert_to_utf8
定义
convert_to_utf8.py:1
convert_to_utf8.process_directory
process_directory(root_dir)
定义
convert_to_utf8.py:27
SurvivalcraftApi 1.8.2.3
convert_to_utf8.py
制作者
1.16.1