Michael
Michael

Reputation: 37

Python Unicode encodings

Is it possible to print Japanese text without changing the system locale on linux? the locale that the Japanese prints on is ja_JP.UTF-8 . But if I set it to decode('utf-8') it will show all weird.

I'm doing: jp_string.decode("GB18030").encode("utf-8")

Here is an image: enter image description here

Upvotes: 0

Views: 98

Answers (3)

jfs
jfs

Reputation: 414215

You can print japanese text without changing your locale if you use some utf-8 locale which supports all Unicode characters. If your locale does not use utf-8 (see the output of locale command) then you could change what character encoding is used for I/O by setting PYTHONIOENCODING envvar:

$ PYTHONIOENCODING=utf-8 python your-script.py

Make sure the text is stored as Unicode (unicode type on Python 2, str (the default) type on Python 3). Print Unicode strings directly, don't encode them to bytes manually.

Upvotes: 0

Michael
Michael

Reputation: 37

It worked. The answer is provided by Jason, by setting the locale to en_US.UTF-8 it displays perfect. Thanks a lot guys. :D

Upvotes: 1

Jasen
Jasen

Reputation: 12402

You appear to be missing some of the neccessary fonts to print Japanese text, in particular UTF-8 appears to be working correctly, just some of the glyphs are missing.

if you can, install, but don't select the japanses locale, that will get you the needed fonts without breaking your current locale.

In windows if you visit some japanese web pages you get a prompt to install the missing font package.

Upvotes: 0

Related Questions