看板 Python 關於我們 聯絡資訊
剛學習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
milua:對了,在eclispe裡面的console端 print是ok@@ 11/17 19:36
danqing:因為中文windows的cmd下只能顯示unicode或cp950(big5) 11/17 19:41
milua:但是我chcp 65001 (unicode) 後執行也是亂碼 @@ 11/17 19:44
milua:把檔案編碼存成ansi就可以正常顯示 11/17 19:45
milua:所以我們用eclispe寫程式的時候 到底要編碼成ansi還是utf8?? 11/17 19:46
danqing:隨便 然後要印出來到cmd時就用unicode吧 11/17 20:36
milua:謝謝回覆喔 11/17 21:33
lovepc:thx 內容很實用~ 11/18 23:09
jbot:cp950 != big5 cp950有多一些微軟獨用的符號 11/25 14:19