当前位置: 动力学知识库 > 问答 > 编程问答 >

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:

网友答案:

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.

网友答案:

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

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: