python utf-8日本语 [英] python utf-8 japanese
问题描述
我有一些日语单词希望转换为utf-8,如下所示:
I have some Japanese words I wish to convert to utf-8, as shown below:
jap_word1 = u'中山'
jap_word2 = u'小倉'
print jap_word1.encode('utf-8') # Doesn't work
print jap_word2.encode('utf-8') # Prints properly
为什么一个单词可以正确转换为utf-8并打印以显示相同的字符,而另一个则不能?
Why is it that one word can be converted properly to utf-8 and printed to show the same characters but not the other?
(我正在Windows 7 Ultimate上使用python 2.6)
(I am using python 2.6 on Windows 7 Ultimate)
推荐答案
很多东西必须对齐才能正确打印字符:
Lots of things must align to print characters properly:
- 脚本保存在什么编码中?
- 脚本中是否有
# coding: xxxx
语句,其中xxxx与保存文件的编码匹配? - 您的输出终端是否支持您的编码?
import sys; print sys.stdout.encoding
一种.如果没有,您可以更改控制台编码吗? (在Windows上为chcp
命令) - 您使用的字体是否支持字符?
- What encoding is the script saved in?
- Do you have a
# coding: xxxx
statement in your script, where xxxx matches the encoding the file is saved in? - Does your output terminal support your encoding?
import sys; print sys.stdout.encoding
a. If not, can you change the console encoding? (chcp
command on Windows) - Does the font you are using support the characters?
将脚本保存为UTF-8,可在PythonWin和IDLE中使用.
Saving the script in UTF-8, this works in both PythonWin and IDLE.
# coding: utf-8
jap_word1 = u'中山'
jap_word2 = u'小倉'
print jap_word1
print jap_word2
有趣的是,我在IDLE的两个打印中添加了.encode('utf-8')
来获得您的结果,但是在Pythonwin中它可以正常工作,其默认输出窗口支持UTF-8.
Interestingly, I got your results with the .encode('utf-8')
added to both prints in IDLE, but it worked correctly in Pythonwin, whose default output window supports UTF-8.
空闲是一种奇怪的野兽.我的系统上的sys.stdout.encoding
生成'cp1252'
,它不支持亚洲字符,但是当以UTF-8打印时,它会打印错误的第一个单词,并输出第二个正确的单词.
Idle is a strange beast. sys.stdout.encoding
on my system produces 'cp1252'
, which doesn't support Asian characters, but it prints the first word wrong and the second one right when printing in UTF-8.
这篇关于python utf-8日本语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!