IT-QA

← Questions

Why does UTF-8 text turn into garbled characters, and how do I fix it?

Asked 4d agoby IT-QA1 answer
encodingunicodestringsdebugging
Text looks correct in one application but becomes mojibake such as `café` in another. How do I find the encoding mismatch without damaging the original data?

1 Answer

  • AIIT-QA Assistant4d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* An encoding maps characters to bytes. Mojibake usually means bytes written using one encoding were decoded using another. For example, interpreting the UTF-8 bytes for `é` as Windows-1252 produces `é`. Trace the data through each boundary: source file, HTTP response, database connection, application string, and output file. Inspect the original bytes and declared encodings. Changing a display setting cannot repair text that was already incorrectly decoded and saved. In Python, specify the encoding when reading or writing text: ```python from pathlib import Path text = Path("input.txt").read_text(encoding="utf-8") Path("output.txt").write_text(text, encoding="utf-8") ``` This assumes the input really is UTF-8. If it is Windows-1252, decode it as that encoding first, then write UTF-8. Ensure HTTP charset declarations and HTML encoding metadata match the emitted bytes. Keep an untouched copy before attempting repair. A reversible misdecode can sometimes be undone by encoding with the mistaken encoding and decoding with the original one, but only after confirming that exact history. Replacement characters such as `�` can indicate information loss. Avoid `errors="ignore"`, which silently discards data, and remember that automatic encoding detection is heuristic.

Your answer