A colleague sends a CSV over WhatsApp. You double-click it and Excel turns the whole screen into alien symbols or rows of question marks. The instinct is to say the file is corrupted and ask for another copy, but the tenth copy looks exactly the same. In most cases the file is perfectly fine. The problem is the encoding table used to read it.
This bites Hong Kong SMEs constantly: accounting exports, shipping lists from mainland factories, customer records dumped out of a legacy system. In Python it takes a handful of lines to fix, and the same approach can convert an entire folder in one go.
What garbled text actually is
Inside a text file, a Chinese character is never a character. It is a sequence of bytes. An encoding table is what turns those bytes into something a human can read. The same character takes three bytes in UTF-8, two bytes in Big5, and a different pair in GBK. Decode with the wrong table and you get characters that do not exist.
Hong Kong runs into four encodings most often:
- UTF-8: the current standard, used by websites, APIs and most new systems.
- Big5: legacy Windows Traditional Chinese, still common in bank and government systems.
- cp950: Microsoft's Big5 implementation, which includes the HKSCS supplementary character set. For old Hong Kong files, use cp950 rather than plain Big5.
- GBK: the mainland China default, and what Simplified Chinese Excel produces when exporting CSV.
Excel adds a separate trap of its own. Double-click a UTF-8 CSV and it garbles even when the file is completely correct, because Excel looks for a BOM (three invisible bytes) at the start. No BOM means it assumes the local ANSI code page, which on Traditional Chinese Windows is Big5. The file is fine; Excel is reading it wrong.
Step one: detect the encoding instead of guessing
The usual mistake is to try big5, then gbk, then start doubting everything. Detect it instead:
percent_chaos is the proportion of characters that came out as garbage. Under 0.1 is generally trustworthy. If several encodings score similarly, read the first five lines with each and see which one produces readable Chinese. Detection is unreliable on very short files, so a one or two line CSV still needs a human look.
Step two: read the file with pandas and an explicit encoding
pandas defaults to UTF-8, so a Big5 or GBK file will always fail. One parameter fixes it:
If a file has one or two bad bytes and you do not want the whole read to fail, tell pandas to substitute a replacement character:
Note that replace only stops the exception. The broken characters become a replacement glyph, so always scan for stray symbols before treating the data as clean.
Step three: write it back as utf-8-sig so Excel opens it properly
A very common story: the converted file reads fine in Python, but the moment you send it to a client and they open it in Excel, it is garbled again, and you conclude the conversion failed. The cause is writing plain UTF-8:
utf-8-sig is UTF-8 with a BOM. Only when Excel sees those three invisible bytes does it treat the file as UTF-8; without them it assumes ANSI and the Chinese turns to garbage. This is the single most common cause of "it broke again after I saved it". Any CSV you hand to another person should be written with utf-8-sig.
If the other side insists on xlsx, just use df.to_excel("clean.xlsx", index=False). It is more robust, because xlsx stores data as XML, which is UTF-8 by definition, so there is no encoding guesswork at all.
Step four: convert an entire folder
In reality it is never one file. A supplier sends thirty CSVs at once, and opening them one by one eats the afternoon. This loop handles the whole folder:
It tries four encodings in order and keeps the first that reads. Note dtype=str: read every column as text rather than letting pandas infer numbers, for the reason below.
There is a blind spot. GBK sometimes decodes without raising an error and still produces the wrong characters. For high volumes or important files, run the detector from step one first and decide on the encoding deliberately, rather than relying on the try loop alone.
Step five: how much of already-garbled text can be recovered
If the damage is classic mojibake, where UTF-8 bytes were read as latin1, it can be reversed:
The idea is to turn the garbled string back into its original bytes, then decode those with the correct table. This only works when nothing else touched the data in between. Once characters have been saved as replacement glyphs, the original bytes are gone for good. Which is why the strategy is always to specify the encoding at the source, not to repair afterwards.
Three pitfalls that show up in Hong Kong
- Leading zeros vanish from phone numbers, long IDs become 1.23E+15: nothing to do with encoding; it is Excel and pandas treating the value as a number. Use
dtype=stron read and keep the output as text, and both phone numbers and order IDs survive intact. - Characters Big5 cannot represent become question marks: colloquial Cantonese characters, Japanese names, variant forms. This is not a code bug; the character simply is not in the Big5 table. Supporting them properly means moving the whole pipeline to UTF-8, not converting half of it.
- One folder, two encodings: the hardest case. You cannot read everything with a single encoding, so check each file. Once checked, standardise on utf-8-sig immediately rather than keeping two sets around.
Summary
Python Tutorial: garbled Chinese is almost always the wrong encoding at read or write time, not a damaged file. Three rules cover it: detect the encoding with charset-normalizer before reading; pass an explicit encoding= to pandas, cp950 for most legacy Hong Kong files and gbk for mainland exports; and always write with utf-8-sig when the file has to open in Excel. For batches, try four encodings per file, standardise the output and keep the originals. Finally, use dtype=str so phone numbers and order IDs are never turned into numbers.