Why Does Python 2 Encoding Fail with Chinese Characters? A Step‑by‑Step Guide
This article explains Unicode and UTF‑8 concepts, then demonstrates in Python 2 on Windows and Linux how encoding and decoding Chinese strings can cause errors, clarifies why str.encode('utf‑8') may still fail, and offers practical solutions for correct string handling.
Previously we introduced the theory of Unicode and UTF‑8 encoding; you can read that article here .
We then demonstrate string encoding in a Python 2 environment on both Windows and Linux to deepen understanding.
1. Windows demonstration
In the Windows command line, entering the strings abc and u'abc' shows that the former is a byte string while the latter is Unicode. Encoding both with utf‑8 works without error.
When using Chinese characters, the byte string raises an error while the Unicode string does not. This occurs because the byte string is encoded as GB2312 on Windows, whereas the Unicode string is already decoded. To convert the GB2312 string to UTF‑8, you must first decode it to Unicode and then encode it to UTF‑8.
2. Linux demonstration
Repeating the same steps on Linux yields the same final result, but the process differs because Linux uses UTF‑8 as the default encoding. Directly encoding a Chinese byte string to UTF‑8 fails; you must decode it using UTF‑8 first, then re‑encode.
The key point is that str1.encode('utf‑8') appears to work only if str1 is already Unicode. In Python 2, encode() first decodes the string using the system’s default codec, which is ASCII on many systems, leading to a decoding error before the UTF‑8 encoding step.
Thus, attempting to encode a Chinese string with ASCII decoding causes an error, and the subsequent encode('utf‑8') never executes.
We conclude the discussion of Python 2 string encoding here and will cover Python 3 encoding issues in the next article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
