[問題] 中文編碼的疑惑..

看板Python作者 (milua)時間16年前 (2008/11/17 19:30), 編輯推噓4(406)
留言10則, 5人參與, 最新討論串1/5 (看更多)
剛學習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

11/17 19:36, , 1F
對了,在eclispe裡面的console端 print是ok@@
11/17 19:36, 1F

11/17 19:41, , 2F
因為中文windows的cmd下只能顯示unicode或cp950(big5)
11/17 19:41, 2F

11/17 19:44, , 3F
但是我chcp 65001 (unicode) 後執行也是亂碼 @@
11/17 19:44, 3F

11/17 19:45, , 4F
把檔案編碼存成ansi就可以正常顯示
11/17 19:45, 4F

11/17 19:46, , 5F
所以我們用eclispe寫程式的時候 到底要編碼成ansi還是utf8??
11/17 19:46, 5F

11/17 20:36, , 6F
隨便 然後要印出來到cmd時就用unicode吧
11/17 20:36, 6F

11/17 21:33, , 8F
謝謝回覆喔
11/17 21:33, 8F

11/18 23:09, , 9F
thx 內容很實用~
11/18 23:09, 9F

11/25 14:19, , 10F
cp950 != big5 cp950有多一些微軟獨用的符號
11/25 14:19, 10F
文章代碼(AID): #198LNjVM (Python)
文章代碼(AID): #198LNjVM (Python)