作者milua (milua)
看板Python
标题[问题] 中文编码的疑惑..
时间Mon Nov 17 19:30:52 2008
刚学习python
print 中文的问题一直困扰着我
但又解决不了
我的ide 是 eclispe + pydev
我写了一个test code为
=========python code=======
#-*- coding: utf-8 -*-
print '科科'
===========================
存档编码,我是选utf8
用windows cmd 执行 出来的结果为
蝘?
如果换成 print u'科科'
就可以了@@ 所以我一直到处找 如何直接 print '科科' 就能正常显示
去python.tw的mail list问 有人回答说 把 lib/site.py里面的
def setencoding(): 改成下面这样就可以
===================================================
def setencoding():
"""Set the string encoding used by the Unicode implementation. The
default is 'ascii', but if you're willing to experiment, you can
change this."""
encoding = "ascii" # Default value set by _PyUnicode_Init()
if 1:
# Enable to support locale aware default string encodings.
import locale
loc = locale.getdefaultlocale()
if loc[1]:
encoding = loc[1]
if 0:
# Enable to switch off string to Unicode coercion and implicit
# Unicode to string conversion.
encoding = "undefined"
if encoding != "ascii":
# On Non-Unicode builds this will raise an AttributeError...
sys.setdefaultencoding(encoding) # Needs Python Unicode build !
===================================================
不过改过了还是不行在windows cmd 底下跑出正确的 '科科' 两个字 >"<
希望有人可以帮我解惑一下 thx
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.134.110.252
1F:→ milua:对了,在eclispe里面的console端 print是ok@@ 11/17 19:36
2F:推 danqing:因为中文windows的cmd下只能显示unicode或cp950(big5) 11/17 19:41
3F:→ milua:但是我chcp 65001 (unicode) 後执行也是乱码 @@ 11/17 19:44
4F:→ milua:把档案编码存成ansi就可以正常显示 11/17 19:45
5F:→ milua:所以我们用eclispe写程式的时候 到底要编码成ansi还是utf8?? 11/17 19:46
6F:→ danqing:随便 然後要印出来到cmd时就用unicode吧 11/17 20:36
8F:→ milua:谢谢回覆喔 11/17 21:33
9F:推 lovepc:thx 内容很实用~ 11/18 23:09
10F:推 jbot:cp950 != big5 cp950有多一些微软独用的符号 11/25 14:19