📦 文件编码转换工具说明
本工具支持将文本文件从一种字符编码转换为另一种字符编码。上传文件后,系统会自动检测文件的 BOM 头并提示可能的编码格式,您也可以手动选择源编码和目标编码进行精确转换。
支持的编码格式
| 分类 | 编码 | 适用场景 |
|---|---|---|
| Unicode | UTF-8 | 最通用的编码,网页、现代应用首选 |
| UTF-16 LE/BE | Windows 系统内部常用 | |
| UTF-32 LE/BE | 固定长度字符编码 | |
| 中文编码 | GBK | 简体中文 Windows 默认编码 |
| GB2312 | 早期简体中文编码 | |
| GB18030 | 国家标准,兼容 GBK | |
| Big5 | 繁体中文编码 | |
| 西欧编码 | ISO-8859-1 | 拉丁字母编码 |
| Windows-1252 | Windows 西欧语言编码 | |
| 日韩编码 | Shift_JIS | 日文编码 |
| EUC-JP | Unix 日文编码 | |
| EUC-KR | 韩文编码 |
常见使用场景
- GBK → UTF-8:将 Windows 下生成的中文文本转换为通用 UTF-8 编码,避免在 Linux/Mac 上出现乱码。
- UTF-8 → GBK:将 UTF-8 文件转换为 GBK,适配某些只支持 GBK 的老旧系统或软件。
- Big5 → UTF-8:将繁体中文文件转换为 UTF-8,方便跨平台使用。
- 去除 BOM:配合 BOM 检测工具,去除 UTF-8 BOM 以避免 PHP、Python 等脚本出现输出问题。
🐍 Python 中编码转换示例
# 读取 GBK 文件并保存为 UTF-8
with open('input.txt', 'r', encoding='gbk', errors='replace') as f:
content = f.read()
with open('output.txt', 'w', encoding='utf-8') as f:
f.write(content)
# 批量转换目录下所有 txt 文件
import os, glob
for filepath in glob.glob('*.txt'):
with open(filepath, 'r', encoding='gbk', errors='replace') as f:
content = f.read()
newpath = filepath.replace('.txt', '_utf8.txt')
with open(newpath, 'w', encoding='utf-8') as f:
f.write(content)
print(f"已转换: {filepath} -> {newpath}")