Hi all,
I am having problem printing chinese character in a text file.

Some point to know:
1. I do not know the codec of the text file, but I know it is writting in traditional chinese.
2. I tried some approach from google, no one work

I don't remember printing chinese character is so damn hard.......
Looking forwards to any help. Thanks

Dani AI

Generated

Building on 's pointer to Unicode basics and 's note about locales, here is a compact troubleshooting checklist and a small workflow to handle an unknown-encoding Traditional Chinese file (for and others landing here later).

Common failure modes: the file bytes use Big5/CP950 (Traditional Chinese) or UTF-8/UTF-16 while the reader/writer uses a different codec; the terminal/editor cannot render the characters even when the file is correct; automatic detectors can misguess on short samples. First check for a BOM (byte-order-mark) in the first bytes (EF BB BF = UTF-8 BOM, FF FE = UTF-16LE, FE FF = UTF-16BE). If no BOM, use an encoding detector and fall back to likely Traditional encodings (Big5/CP950 or Big5-HKSCS).

Example workflow (Python 3; requires the chardet package):

import chardet

with open('input.txt', 'rb') as f:
    raw = f.read()

enc = chardet.detect(raw).get('encoding') or 'utf-8'
text = raw.decode(enc, errors='replace')

with open('output-utf8.txt', 'w', encoding='utf-8') as f:
    f.write(text)

For Python 2, decode the raw bytes and write with codecs.open(..., 'w', 'utf-8'). If chardet fails or guesses poorly, try big5, cp950, big5hkscs, or gb18030 explicitly. Prefer saving output as UTF-8 and opening it in a Unicode-aware editor. On Windows, the console often uses an OEM code page and may not display Chinese even when the file is correct; opening the file in Notepad++/Sublime/VSCode (explicit UTF-8) or switching the console code page can help.

Useful references: the chardet package for detection (chardet on PyPI) and the Unicode BOM FAQ (unicode.org BOM FAQ).

Recommended Answers

All 2 Replies

You must also have locales for chinese install on your system
;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.