简体   繁体   English

python unicode转换为日语字符

[英]python unicode convert to Japanese character

I am trying to convert u'\ド\ラ\ゴ\ン' to japanese character using python 我正在尝试使用python将u'\\ u30c9 \\ u30e9 \\ u30b4 \\ u30f3'转换为日语字符

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) 我收到此错误UnicodeEncodeError:'ascii'编解码器无法在位置0-3处编码字符:序数不在范围内(128)

This will depend on your OS and configuration, but normally, you just print the Unicode string. 这将取决于您的操作系统和配置,但是通常情况下,您只需要打印Unicode字符串即可。 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. 在Linux上,这要求您的终端已正确配置为(通常)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. 在Windows上,您需要一个支持UTF-8的IDE,但是如果使用Windows控制台,除非使用支持日语的本地化版本的Windows或将系统区域设置更改为日语,否则您将收到UnicodeEncodeError Another workaround is to use win-unicode-console and install a Japanese console font. 另一个解决方法是使用win-unicode-console并安装日语控制台字体。

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. 上面的示例使用了pywin32模块随附的PythonWin IDE,并且还可以在标准Python安装随附的Python IDLE IDE中使用。

I had an UnicodeEncodeError for Japanese characters in REPL on Windows 10. 我在Windows 10的REPL中遇到了日语字符的UnicodeEncodeError

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. Beta:使用Unicode UTF-8获得全球语言支持。

I checked this option on with leaving the current system locale as English (ie, unchanged). 我选中了此选项,而将当前系统区域设置保留为英语(即不变)。
After reboot, REPL started to print Japanese characters correctly. 重新启动后,REPL开始正确打印日语字符。

You get s in bytes. 您获得s的字节数。 To get the Japanese characters, use print(s.decode('utf-8')) . 要获取日语字符,请使用print(s.decode('utf-8'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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