2013-11-27 55 views
1

我的web應用程序之前跑得非常好,但日前一個問題就來了,現在我可以開始我的web應用程序,但是當我瀏覽我的從本地(127.0.0.1)站點或遠程(192.168.xxx.xxx)(僅簡單地打開網頁,無論從鼠標和鍵盤輸入),crashs這樣的web應用程序:的UnicodeDecodeError:「UTF-8」編解碼器不能在5位解碼字節0xcb:無效延續字節

Traceback (most recent call last): 
File "/path/to/project/web/application.py", line 242, in process 
    return self.handle() 
File "/path/to/project/web/application.py", line 233, in handle 
    return self._delegate(fn, self.fvars, args) 
File "/path/to/project/web/application.py", line 415, in _delegate 
    return handle_class(cls) 
File "/path/to/project/web/application.py", line 390, in handle_class 
    return tocall(*args) 
File "./my_web_app.py", line 40, in GET 
    simplejson.dumps(manus)) 
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 286, in dumps 
    return _default_encoder.encode(obj) 
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 226, in encode 
    chunks = self.iterencode(o, _one_shot=True) 
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 296, in iterencode 
    return _iterencode(o, 0) 
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 5: invalid continuation byte 
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /" - 500 Internal Server Error 
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /favicon.ico" - 404 Not Found 
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /favicon.ico" - 404 Not Found 

,我不認爲有我的代碼有些問題,因爲我的代碼在我的計算機上運行得非常好,只有當它在服務器上運行時纔會出現錯誤。 「web」目錄是「web.py-0.34/web」的鏈接,它不是我的代碼。

我的代碼很簡單:

urls = (
    '/', 'find_alternate', 
    '/find_alternates', 'find_alternate', 
    '/show_detail/(.+)', 'show_detail' 
) 
app = web.application(urls, globals()) 
class find_alternate: 
    def GET(self): 
     brands = [b.brandName for b in Brand.q.all()] 
     brands.sort() 
     manus = [oe.brandName for oe in OeNumber.q.group_by(OeNumber.brandName)] 
     manus.sort() 
     return render.find_alternates_main(simplejson.dumps(brands), simplejson.dumps(manus)) 
""" 
some more functions, but not relevant 
""" 
render = web.template.render('/path/to/templates/') 
web.template.Template.globals['str'] = str 
if __name__ == "__main__": 
    app.run() 

我的CREATE TABLE:現在

CREATE TABLE `brand` (
    `brandNo` int(11) NOT NULL, 
    `brandName` varchar(64) DEFAULT NULL, 
    PRIMARY KEY (`brandNo`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 

我的問題是藿字符Ë從Unicode轉換爲UTF-8,這樣jsonsimple可以解析它。在維基我發現這一點:

Unicode: U+00CB 
UTF-8: C3(hex) 8B(hex) 

如何固溶: 添加以下行my.cnf文件:

collation-server = utf8_unicode_ci 
init_connect='SET NAMES utf8' 
character-set-server = utf8 
skip-character-set-client-handshake 

數據庫轉換爲UTF-8:

ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin; 
+0

「我不認爲有什麼問題」是不夠的。請寄出重要的一段代碼。 – jazzpi

+0

顯然,你的代碼或者你的web服務器配置,或者你訪問它的方式都有問題。既然你沒有給我們任何關於這三者的信息,那麼任何人都無法幫助你。除了給你一些提示,你可能出於某種原因發送非UTF-8數據(例如Latin-1或Windows CP1252),並且你的代碼或web.py試圖將它解釋爲UTF-8原因。如果你還沒有閱讀[Unicode HOWTO](http://docs.python.org/2/howto/unicode.html),現在可能是個好時機。 – abarnert

+0

sry,我沒有說完我的話和missclicked「post」 – Cricket

回答

1

u'\xcb'是的'\xc3\x8b'的Unicode表示,

>>> u'CITRO\xcbN'.encode('utf-8') 
'CITRO\xc3\x8bN' 

及其latin-1編碼:

>>> u'CITRO\xcbN'.encode('latin-1') 
'CITRO\xcbN' 

所以,你的服務器DB似乎不是UTF-8編碼。

我認爲最好的解決方案是檢查你的服務器表編碼,如果它不是utf8,遷移到utf8。如果表格使用utf8,則必須修復數據,因爲數據不是。

或者,你可以推斷出從數據庫設置編碼,並傳遞給simplejson:

simplejson.dumps(manus, encoding=encoding) 

但這種方法會導致未來的服務器和DEV和錯誤之間的區別。

+0

我讀完這個單詞後可以將它轉換爲utf8嗎? – Cricket

+0

@Cricket查看更新 – alko

+0

我已將服務器數據庫中的數據轉換爲utf-8,並將錶轉儲爲mysqldump,然後使用hexdump進行檢查,結果與本地服務器相同,但確實是utf-8,但遇到了另一個問題python app讀取數據後,數據已經改爲非utf-8。連接字符集問題?我在谷歌找不到解決方案。 – Cricket

相關問題