← 返回
未分类

Charset Fix

Fix Chinese/Unicode character encoding issues when running AI agents on Windows via POSIX shells (Git Bash, MSYS2, WSL, BusyBox, etc.). Handles Python, Power...
解决在 Windows 上通过POSIX shell(Git Bash、MSYS2、WSL、BusyBox 等)运行 AI 代理时的中文/Unicode 编码问题,支持 Python、PowerShell 等。
gkd2323c
未分类 clawhub v1.0.0 1 版本 99532.7 Key: 无需
★ 0
Stars
📥 213
下载
💾 0
安装
1
版本
#latest

概述

Windows Character Encoding Fix for AI Agents

> Fix garbled Chinese/Unicode text output when running AI agents on Windows through POSIX-compatible shells.

Problem

When AI agents run commands on Windows through POSIX shells (Git Bash, MSYS2, BusyBox, or any Unix-like shell layer), text output containing Chinese or extended Unicode characters often appears garbled:

$ python3 -c "print('中文测试')"
���IJ���  ← garbled
$ echo "中文测试"
中文测试       ← correct

Root Cause

LayerEncodingWhy
----------------------
Windows systemGBK/GB2312 (CP936)Default code page for Chinese Windows
Python 3GBKsys.stdout.encoding auto-detects system code page
PowerShell (powershell.exe)GB2312[Console]::OutputEncoding defaults to system CP
cmd.exeGBKNative Windows command processor
POSIX shell (Git Bash, BusyBox, MSYS2)UTF-8Expects UTF-8 input
PowerShell Core (pwsh.exe)UTF-8✅ Defaults to UTF-8, no fix needed

The mismatch: Windows-native tools output GBK-encoded text, but the POSIX shell terminal reads it as UTF-8, producing garbled characters.

Quick Fix

Python

PYTHONIOENCODING=utf-8 python3 -c "print('中文测试 ✅')"

Set it for the whole session:

export PYTHONIOENCODING=utf-8
python3 script.py

PowerShell (Windows PowerShell, not Core)

powershell.exe -NoProfile -Command "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Write-Host '中文测试'"

cmd.exe / Windows native tools

Use Python's subprocess as a GBK→UTF-8 bridge:

PYTHONIOENCODING=utf-8 python3 -c "
import subprocess
r = subprocess.run(['cmd.exe', '/c', 'systeminfo | findstr 系统'],
    capture_output=True, text=True, encoding='gbk')
print(r.stdout)
"

Code-level fix (Python)

import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

Verification

PYTHONIOENCODING=utf-8 python3 -c "print('charset-fix: 中文测试成功 ✅')"

Expected: charset-fix: 中文测试成功 ✅

How It Works

FixMechanism
----------------
PYTHONIOENCODING=utf-8Overrides Python's stdout encoding detection
[Console]::OutputEncoding = UTF8Sets PowerShell's console output to UTF-8
subprocess(..., encoding='gbk')Decodes cmd.exe output correctly, then emits as UTF-8

Compatibility

PlatformStatus
------------------
Windows + Git Bash✅ Works
Windows + BusyBox✅ Works
Windows + MSYS2✅ Works
Windows + WSL✅ Works
macOS / Linux⬜ Not needed
PowerShell Core (pwsh)⬜ Not needed

Works with: Claude Code, Codex CLI, Cline, Cursor, GitHub Copilot, OpenClaw agents.

Debugging

# Check code page
powershell.exe -NoProfile -Command "chcp"

# Check Python encoding
python3 -c "import sys; print(sys.stdout.encoding)"

# Test raw shell output
echo "中文测试"

License

MIT

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-21 15:43 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-intelligence

self-improving agent

pskoett
捕获经验教训、错误及修正内容,以实现持续改进。适用于以下场景:(1)命令或操作意外失败;(2)用户纠正Claude(如“不,那不对……”“实际上……”);(3)用户请求的功能不存在;(4)外部API或工具出现故障;(5)Claude发现自身
★ 4,071 📥 804,668
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 672 📥 324,936
security-compliance

Skill Vetter

spclaudehome
AI智能体技能安全预审工具。安装ClawdHub、GitHub等来源技能前,检查风险信号、权限范围及可疑模式。
★ 1,223 📥 267,309