今天在學習寫wxPython時,終於遇到了python中“歷史悠久”的編碼方式的問題,很普遍,但是我不會,也相信很多新手也不會,呵呵,故記錄之,也想做這樣一個簡單的系列文章,希望能夠把編碼方式的問題搞清楚。
IDE使用國人python高手的傑作UliPad ,作者:limodou. 先看源代碼:
==>Main.py
#!/usr/bin/env python
#coding=utf-8
import wx
class MainFrame(wx.Frame):
""" """
def __init__(self):
wx.Frame.__init__(self, None, -1, "Main Frame", size=(300, 300))
panel = wx.Panel(self,-1)
wx.StaticText(panel,-1,"圖片路徑:",pos=(10,12))
self.left,self.top,self.left,self.top = 0,0,0,0 # set the init mouse value
def main():
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show(True)
app.MainLoop()
main()
執行後提示錯誤:
UnicodeDecodeError: 'cp950' codec can't decode bytes in position 0-1: illegal multibyte sequence
從字面看應該是編碼出了問題,google後發現解決方法 ,沒有很深入的探究,再說一深入我就暈,呵呵,先解決問題再說.
解決方案:"圖片路徑:".decode('utf-8').encode('cp950')