2015-08-18 81 views
2

我正在用燒瓶寫一個小網頁,我偶然發現了奇怪的錯誤。燒瓶形式數據類型錯誤(unicode數據不是unicode)

以簡單的形式,我想傳遞3個字符串值。當我試圖通過任何特殊字符(如「L」,「A」,等等),瓶給我的回溯:

127.0.0.1 - - [18/Aug/2015 16:16:32] "POST /add_tutorial HTTP/1.1" 500 - 
Traceback (most recent call last): 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ 
return self.wsgi_app(environ, start_response) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/werkzeug/contrib/fixers.py", line 148, in __call__ 
return self.app(environ, start_response) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app 
response = self.make_response(self.handle_exception(e)) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception 
reraise(exc_type, exc_value, tb) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app 
response = self.full_dispatch_request() 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request 
rv = self.handle_user_exception(e) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception 
reraise(exc_type, exc_value, tb) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request 
rv = self.dispatch_request() 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request 
return self.view_functions[rule.endpoint](**req.view_args) 
    File "/home/pawel/repos/tiktalik-www-panel/panel.py", line 123, in add_tutorial 
author=data['author'] 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/writing.py", line 784, in add_document 
perdocwriter.add_column_value(fieldname, column, cv) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/codec/base.py", line 821, in add_column_value 
self._get_column(fieldname).add(self._docnum, value) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/columns.py", line 251, in add 
self._dbfile.write(v) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/filedb/structfile.py", line 99, in write 
return self.file.write(*args, **kwargs) 
    File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/filedb/compound.py", line 325, in write 
bio.write(inbytes) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in position 10: ordinal not in range(128) 

其崩潰的代碼:

def add_tutorial(): 
if request.method == 'POST': 
    ix = indexing(INDEXDIR,BaseSchema,'tutorials') 
    with ix.writer(optimize = True) as writer: 
     data = {} 
     data['title'] = request.form['title'] #here it crashes 
     app.logger.debug(type(request.form['title'])) 
     data['author'] = request.form['author'] 
     data['difficulty'] = int(request.form['difficulty']) 
     data['fileOfArt'] = request.files['file'] 
     data['pictures'] = request.files.getlist("pictures") 
     data['lang'] = request.form['lang'] 
     data['tags'] = parse_tags(request.form['tags']) 
     if data['title'] and data['fileOfArt']: 
      subdir = plToAng(data['title']) 
      subdir = subdir.replace(" ","_") 
      content = data['fileOfArt'].read() 
      for picture in data['pictures']: 
       handle_file(picture,subdir) 
      content = repl_pictures(content,data['pictures'],subdir) 
      writer.add_document(
        title=data["title"], 
        sort_title=data["title"].lower(), 
        content=content, 
        date=datetime.now(), 
        url=subdir, 
        difficulty=data['difficulty'], 
        lang=data['lang'], 
        tags=data['tags'], 
        author=data['author'] 
      )  
     ix.close() 
     return redirect(url_for('add_tutorial', title=data['title'],\ 
      difficulty=data['difficulty'])) 

當我嘗試強制request.form ['標題']編碼爲「UTF-8」我得到錯誤,「TypeError:解碼Unicode不支持」(邏輯,因爲數據傳遞是Unicode)。

有沒有解決此問題的方法? (我不能放棄特殊字符)

+1

這似乎並非是一個ISSU e與Flask,但與飛快。請發佈*完整回溯*。 –

+0

我已經編輯完全回溯而不是錯誤的帖子 –

+0

所有'data'成員的類型是什麼? –

回答

-1

好吧,我設法將此代碼添加到.py文件的開頭,以解決此問題:

import sys 
reload(sys) 
sys.setdefaultencoding('utf-8') 
+0

這是一個醜陋的黑客。您必須「重新加載()」模塊,因爲Python開發人員顯式地使'setdefaultencoding()'不可用,所以沒有人真正在'site'模塊外使用它。您應該瞭解unicode和編碼,並在您的程序中正確處理。 – BlackJack

0

你應該編碼具有特殊字符作爲utf-8字節字符串字符串:

mynewstring = mystring.encode('utf-8') 

結果會是這樣:

>>> a = u"عاصم" 
>>> a.encode('utf-8') 
'\xd8\xb9\xd8\xa7\xd8\xb5\xd9\x85' 
+0

不幸的是,這種方法不適用於我(我得到「無法解碼unicode」錯誤) –

+1

如果您使用'decode',但我建議編碼,您將會得到此錯誤。 –

相關問題