Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 262
Min Min Avatar asked Nov 24 '25 22:11

Min Min


1 Answers

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.

like image 122
Mark Tolonen Avatar answered Nov 27 '25 10:11

Mark Tolonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!