Claude fix unicode decode
Diagnose and fix UnicodeDecodeError, UnicodeEncodeError, mojibake, and corrupted Chinese text in Claude Code tasks. Use this skill whenever a file, command, test, CSV, JSON, HTTP response, Git diff, or Windows terminal shows broken Chinese, replacement characters, strings such as "䏿–‡" or "涓枃", an unknown codec, or any decode/encode exception. Use it even when the user only says Chinese text is garbled or a file cannot be read.From its SKILL.md
npx -y skills add hachiwar/codex-skills --skill claude-fix-unicode-decodeAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 2 stars2 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
4.2 KB, 832 tokens by cl100k_base, as published. Nobody here has run it
Fix Unicode Decode in Claude Code
Treat encoding as a byte-to-text contract. Identify the original bytes, producer, and actual codec before editing. Do not suppress errors with errors="ignore", and do not overwrite a file after decoding it with an unverified codec.
Diagnose
-
Inspect Git status and preserve the original bytes. Do not revert unrelated user changes.
-
Capture the full exception, codec name, byte offset, failing operation, platform, and exact file or process boundary.
-
Locate the first bytes-to-text conversion:
open,Path.read_text, parser, subprocess, HTTP client, database driver, or terminal. -
Do not infer the source encoding from Claude Code's
Readoutput alone because the display has already crossed a decoding boundary. -
Use Bash or PowerShell to inspect raw bytes and run the bundled read-only diagnostic:
python scripts/diagnose_encoding.py path/to/fileIf the current directory is not this skill directory, resolve the script from the loaded skill's directory first.
-
Prefer evidence in this order: producer or format contract, BOM, protocol declaration, strict decode success, then content plausibility. A successful decode alone is not proof; codecs such as
latin-1andgb18030accept many byte sequences. -
Reproduce with strict decoding and a representative sample containing Chinese, ASCII, punctuation, and an emoji when the format supports it.
Repair
- Fix the narrowest boundary that owns the wrong assumption. Add an explicit
encoding=, configure the child process or terminal, or correct a producer that declares the wrong charset. - Prefer UTF-8 for new repository text and
encoding="utf-8"for Python file I/O. - Use
utf-8-sigonly for a verified UTF-8 BOM or an explicit interoperability requirement such as some Excel CSV workflows. - Use
gb18030for legacy Simplified Chinese only when provenance or byte evidence supports it. Prefer it overgbkfor broader character coverage. - Use
utf-16when a BOM exists. Without a BOM, establish endianness from the producer or format. - Keep byte values as bytes until the intended decoding boundary. Decode once and encode once.
- Use
errors="replace"only for deliberate lossy display or telemetry, and disclose data loss. - Do not promise recovery after
U+FFFDreplacement characters or ignored bytes appear unless original bytes or a backup exists.
from pathlib import Path
text = Path(path).read_text(encoding="utf-8")
Path(path).write_text(text, encoding="utf-8", newline="\n")
result = subprocess.run(
command,
capture_output=True,
text=True,
encoding="utf-8", # Match the child's documented output contract.
errors="strict",
check=True,
)
Read references/encoding-playbook.md for the relevant CSV, JSON, HTTP, database, Windows console, Git migration, or mojibake section. Do not load unrelated sections unless needed.
Verify
- Confirm the original failing input now decodes with
errors="strict". - Assert semantic text such as
中文,编码测试。; checking only that no exception occurs is insufficient. - Run the smallest relevant tests, then inspect
git diff --word-diffandgit diff --checkfor replacement characters, newline churn, or unrelated rewrites. - Report the source codec, target codec, supporting evidence, changed boundary, and any lossy operation.
Diagnostic Output
Use --json for machine-readable output and --encodings for producer-specific candidates. Treat candidate scores as hints rather than detection guarantees. For ambiguous valid results such as GB18030 versus Big5, ask for provenance or compare known text instead of selecting the top score blindly.
What ships with it: 2 files
10.6 KB alongside SKILL.md, 1 of them executable
references/
- encoding-playbook.md5.1 KB
scripts/
- diagnose_encoding.pyruns5.5 KB