简体   繁体   中英

python unicode convert to Japanese character

I am trying to convert u'\ド\ラ\ゴ\ン' 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)

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.

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 (ie, unchanged).
After reboot, REPL started to print Japanese characters correctly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM