Min Min
Min Min

Reputation: 135

python unicode convert to Japanese character

I am trying to convert u'\u30c9\u30e9\u30b4\u30f3' to japanese character using python

here is my sample code

s = u'\u30c9\u30e9\u30b4\u30f3'.encode('utf-8')
print str(s)

I got this error UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

Upvotes: 3

Views: 12194

Answers (3)

vjou
vjou

Reputation: 103

I had an UnicodeEncodeError for Japanese characters in REPL on Windows 10.

I followed Mark Tolonen's suggestion and went to

Change system locale

in the Region settings. There was an option that said

Beta: Use Unicode UTF-8 for worldwide language support.

I checked this option on with leaving the current system locale as English (i.e., unchanged).
After reboot, REPL started to print Japanese characters correctly.

Upvotes: 2

Mark Tolonen
Mark Tolonen

Reputation: 177610

This will depend on your OS and configuration, but normally, you just print the Unicode string. If your OS, default terminal encoding, and font support Japanese, you only need:

>>> s = u'\u30c9\u30e9\u30b4\u30f3'
>>> print s
ドラゴン

On Linux, this requires your terminal to be properly configured to (typically) UTF-8.

On Windows, you need an IDE that supports UTF-8, but if using the Windows console, you will get a UnicodeEncodeError unless using a localized version of Windows that supports Japanese, or changing the system locale to Japanese. Another workaround is to use win-unicode-console and install a Japanese console font.

My example above used the PythonWin IDE that comes with the pywin32 module, and also works in the Python IDLE IDE that comes with a standard Python installation.

Upvotes: 3

hcheung
hcheung

Reputation: 4024

You get s in bytes. To get the Japanese characters, use print(s.decode('utf-8')).

Upvotes: 0

Related Questions