Your file can be valid UTF-8 and still display Café. That happens when the original UTF-8 bytes for Café were decoded as Windows-1252, then the resulting wrong characters were saved as UTF-8. The browser is now reading the saved bytes correctly; the text itself is already wrong.
Problem
The key detail is that this failure leaves a recognizable trace. The original UTF-8 bytes for é are C3 A9. Read as Windows-1252, those bytes become the characters é. Saving that text as UTF-8 writes new bytes for those two characters. A <meta charset="UTF-8"> declaration can correctly describe the new file, but it cannot infer that é used to be é.
# Text in the damaged file: Café # The inverse of the mistaken decode: "Café".encode("cp1252").decode("utf-8") # 'Café' This is not a general rule that every occurrence of à is corrupt. The same characters can appear legitimately, and not every mojibake sequence has this exact history. The conversion is justified only when you have evidence that this particular text was UTF-8 misread as Windows-1252. Compare it with a trusted source, translation memory, or an unaffected copy before changing anything.
The distinction between a declaration and a conversion is the central point in the walkthrough on diagnosing and repairing mojibake in HTML. For this specific pattern, think of the repair as reversing one known, mistaken decoding step—not as changing the document's charset.
Fix
Repair the known damaged text, not the entire file. A broad "convert every string containing Ã" pass can alter legitimate content. Even applying the inverse conversion to every character in a document can fail on characters outside Windows-1252 or change unrelated text. Start with an exact, reviewed phrase and make the script fail if that phrase is absent or appears an unexpected number of times.
from pathlib import Path source = Path("page.html") output = Path("page.repaired.html") text = source.read_bytes().decode("utf-8") damaged = "Café" expected = "Café" # Reverse the specific cp1252-read / UTF-8-bytes mistake. repaired = damaged.encode("cp1252").decode("utf-8") if repaired != expected: raise ValueError(f"Unexpected repair result: {repaired!r}") count = text.count(damaged) if count != 1: raise ValueError(f"Expected one reviewed match, found {count}") output.write_bytes(text.replace(damaged, repaired).encode("utf-8")) print(f"Wrote {output}") Writing to a separate output file gives you a simple rollback path and keeps the original available for comparison. If the same reviewed phrase appears in multiple places, decide explicitly whether every occurrence should change; adjust the count check only after reviewing those locations. For a larger batch, keep a mapping of confirmed source strings to repaired strings and report every replacement instead of silently sweeping for suspicious markers. The repair_mojibake repository is useful context for treating this as a targeted conversion rather than a charset declaration change.
After the edit, inspect the output as text and confirm it still decodes as UTF-8. Then check the served page, not just a local editor preview. A correct response charset is still important, but it answers a different question: how should the browser decode the bytes it receives? It does not establish that the characters stored in those bytes are the intended ones.
Production rule: keep the original file and write repairs to a separate output until the changed text has been reviewed.
Production rule: never apply the cp1252-to-UTF-8 reversal to every occurrence of
Ãwithout confirming the source and expected result.
Once the content is correct, keep the normal pipeline consistent: save the HTML as UTF-8 and serve it with a matching UTF-8 charset. That prevents a repeat of the mistake; it does not replace the targeted repair for text that was already misdecoded and saved.