在中文 Windows 环境里,所谓 **ANSI** 通常对应
在中文 Windows 环境里,所谓 **ANSI** 通常对应:
```python
encoding="gbk"
```或者:
```python
encoding="cp936"
```二者基本等价。更通用一点也可以用:
```python
encoding="mbcs"
```---
## 推荐代码:ANSI TXT 转 UTF-8
新建一个脚本,例如:
```python
ansi_to_utf8.py
```代码如下:
```python
input_path = r"D:\Project_2026\原始流水.txt"
output_path = r"D:\Project_2026\原始流水_utf8.txt"source_encoding = "gbk" # Windows 中文 ANSI 通常是 gbk / cp936
target_encoding = "utf-8"line_count = 0
with open(input_path, "r", encoding=source_encoding, errors="replace") as f_in, \
open(output_path, "w", encoding=target_encoding, newline="") as f_out:for line in f_in:
f_out.write(line)
line_count += 1if line_count % 100000 == 0:
print(f"已转换 {line_count} 行")print(f"转换完成,共转换 {line_count} 行")
print(f"输出文件:{output_path}")
```运行:
```powershell
python ansi_to_utf8.py
```---
## 如果你希望转换成 UTF-8 with BOM
有些 Windows 软件,例如 Excel,更喜欢 UTF-8 with BOM。可以把:
```python
target_encoding = "utf-8"
```改成:
```python
target_encoding = "utf-8-sig"
```完整关键部分:
```python
target_encoding = "utf-8-sig"
```区别如下:
| 编码 | 适用情况 |
|---|---|
| `utf-8` | Python、数据库、程序处理更推荐 |
| `utf-8-sig` | Excel、记事本打开更友好 |如果你是为了后续 `pandas.read_csv()` 处理,建议用:
```python
utf-8
```如果你是为了 Excel 打开不乱码,建议用:
```python
utf-8-sig
```---
## 更稳妥版本:自动尝试 ANSI 编码
如果不确定 ANSI 到底是 `gbk`、`cp936` 还是系统默认编码,可以用这个版本:
```python
import osinput_path = r"D:\Project_2026\原始流水.txt"
output_path = r"D:\Project_2026\原始流水_utf8.txt"candidate_encodings = ["gbk", "cp936", "gb18030", "mbcs"]
for source_encoding in candidate_encodings:
try:
print(f"正在尝试源编码:{source_encoding}")line_count = 0
with open(input_path, "r", encoding=source_encoding) as f_in, \
open(output_path, "w", encoding="utf-8", newline="") as f_out:for line in f_in:
f_out.write(line)
line_count += 1if line_count % 100000 == 0:
print(f"已转换 {line_count} 行")print(f"转换成功,源编码:{source_encoding}")
print(f"共转换 {line_count} 行")
print(f"输出文件:{output_path}")
breakexcept UnicodeDecodeError as e:
print(f"编码 {source_encoding} 失败:{e}")if os.path.exists(output_path):
os.remove(output_path)else:
print("所有候选编码都失败,请检查文件是否损坏或是否为其他编码")
```不过对中文 Windows 的 ANSI 文件,通常直接用:
```python
encoding="gbk"
```就可以。
---
## 重要提醒:不要覆盖原文件
不建议直接把原文件覆盖掉,比如不要这样:
```python
input_path = output_path
```建议先输出成新文件:
```python
原始流水_utf8.txt
```确认没问题后,再手动替换原文件。
---
## 转换后用 pandas 读取
转换完成后,你就可以这样读取:
```python
import pandas as pdtxt_path = r"D:\Project_2026\原始流水_utf8.txt"
for chunk in pd.read_csv(
txt_path,
sep=",", # 根据你的文件分隔符调整
encoding="utf-8",
chunksize=100000,
dtype=str
):
print(chunk.head())
break
```如果你转成的是 `utf-8-sig`,也可以这样读:
```python
encoding="utf-8-sig"
```---
## 如果只是为了导入 SQLite,其实不一定要先转码
你也可以直接用 ANSI 编码读取,然后导入 SQLite:
```python
import pandas as pd
import sqlite3txt_path = r"D:\Project_2026\原始流水.txt"
db_path = r"D:\Project_2026\流水.db"conn = sqlite3.connect(db_path)
total = 0
for chunk in pd.read_csv(
txt_path,
sep=",",
encoding="gbk",
chunksize=100000,
dtype=str
):
chunk.to_sql(
"transactions",
conn,
if_exists="append",
index=False
)total += len(chunk)
print(f"已导入 {total} 行")conn.close()
print("导入完成")
```也就是说:
- 如果只是 Python 处理:可以直接 `encoding="gbk"`;
- 如果想统一文件编码:再转成 UTF-8。
相关推荐
ANSI链接中文转码工具ENcode
ANSI、UTF链接中文转码工具,可以支持快速的将类似于百度搜索链接地址中的中文转码转换。 支持ANSI、UTF转码和转中文。 支持剪贴板信息直接粘贴转码。
如何解决 Kotlin/Native 在 Windows 下 main 函数的 args 乱码?
Kotlin/Native 在 Windows 平台处理命令行中文参数时会出现乱码问题,原因是默认使用 ANSI 编码的 API 而非 Unicode 版本。解决方案是通过 GetCommandLineW 和 CommandLineToArgvW 获取 UTF-16 编码的参数,再转换为正确的字符串。
WIN10中文乱码修复合集
全CSDN最全的中文乱码修复的参阅合集
Codex Windows安装配置全指南:本地AI协作基础设施搭建
Codex并非传统CLI工具,而是一套运行在Windows平台上的本地AI协作基础设施,其核心是CLI调度器与可插拔本地推理服务(如Ollama、DeepSeek-Coder)的协同架构。它基于Rust构建,强调数据不出域、模型可替换、指令可审计,技术原理涉及服务发现、HTTP本地回环通信、TLS严格校验及UTF-8终端渲染链路。其技术价值在于为企业提供可控、离线、可审计的代码智能增强能力,广泛应用于私有代码审查、合规开发辅助与AI原生工作流集成。本文聚焦Windows环境下Codex CLI本体部署、Ba
Hyper Termina English ANSI + 超级终端 中文版
English ANSI version of Hyper Termina + 中文Unicode版本的超级终端


