2012-02-10 24 views
3

與Python的Unicode問題一樣,任何Python開發人員現在都已經搞亂了很多年。 但是現在我遇到了一個讓我瘋狂的情況,我無法自己解決這個問題。 它已經要1天,現在,包括RECHERCHES ..Django&Suds:使用QuerySets時的UnicodeEncodeError

我的設置是一個小的Django應用程序,通過SOAP連接到遠程系統(使用肥皂水),拉一些數據和Django的數據庫中尋找它:

from myapp.models import Customer 
client = suds.client.Client(...) 
customer = client.service.getCustomerByEmail('[email protected]') 

type(customer.email): <class 'suds.sax.text.Text'> 

customer_exists = Customer.objects.filter(email=customer.email) 

現在客戶的電子郵件地址有一個德國元音ü,它可以讓Django的拋出異常如下:

Traceback (most recent call last): 
    File "run_anatomy_client.py", line 19, in <module> 
    print client.main() 
    File "/Users/user/Documents/workspace/Wawi/application/myapp/client.py", line 282, in main 
    if not Customer.objects.filter(email=customer.email.encode('latin1')): 
    File "/Users/user/Documents/workspace/Wawi/application/myapp/client.py", line 76, in sync_customer 
    if not customer_exists: 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 113, in __nonzero__ 
    iter(self).next() 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 107, in _result_iter 
    self._fill_cache() 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 772, in _fill_cache 
    self._result_cache.append(self._iter.next()) 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/query.py", line 273, in iterator 
    for row in compiler.results_iter(): 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter 
    for rows in self.execute_sql(MULTI): 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/user/Documents/workspace/Wawi/pyenv/lib/python2.7/site-packages/django/db/backends/util.py", line 43, in execute 
    logger.debug('(%.3f) %s; args=%s' % (duration, sql, params), 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 28: ordinal not in range(128) 

我已經與編碼(),解碼()出場,改變了源文件的編碼以及數據庫l ayout,目前看起來如下: - :

mysql> show variables like '%character%'; 
+--------------------------+-----------------------------------------+ 
| Variable_name   | Value         | 
+--------------------------+-----------------------------------------+ 
| character_set_client  | latin1         | 
| character_set_connection | latin1         | 
| character_set_database | utf8         | 
| character_set_filesystem | binary         | 
| character_set_results | latin1         | 
| character_set_server  | latin1         | 
| character_set_system  | utf8         | 
| character_sets_dir  | /opt/local/share/mysql5/mysql/charsets/ | 
+--------------------------+-----------------------------------------+ 
8 rows in set (0.00 sec) 

奇怪的是,如果我設置一個跟蹤點和執行Django的外殼相同的一行,然後用編碼()時工作得很好

(Pdb) Customer.objects.filter(email=customer.email) 
*** UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 28:  ordinal not in range(128) 
(Pdb) Customer.objects.filter(email=customer.email.encode('utf-8')) 
[] 

我很感謝任何提示..

+0

嗨,你可以嘗試unicode(customer.email,'iso8859-1'),讓我知道它是否有幫助... – Jingo 2012-02-10 22:24:06

+0

Th但它沒有: 如果不是的話。Customer.objects.filter(email = unicode(customer.email,'iso8859-1')): TypeError:解碼不支持Unicode – 2012-02-11 07:44:08

+0

你能解決這個問題嗎?看起來像我面臨着同樣的問題 – Fedor 2014-03-26 09:19:32

回答

2

suds.sax.text.Text從Unicode繼承

class Text(unicode): 
    """ 
    An XML text object used to represent text content. 
    @ivar lang: The (optional) language flag. 
    @type lang: bool 
    @ivar escaped: The (optional) XML special character escaped flag. 
    @type escaped: bool 
    """ 

如果你想使用,你可以編碼爲UTF-8。

email = customer.email.encode("utf-8") 
customer_exists = Customer.objects.filter(email=email) 
+0

我認爲它會更好地使用unicode(客戶),而不是customer.encode('utf-8') – Fedor 2014-03-28 11:51:16

+0

我有*完全相同的問題。非常感謝! – 2014-09-30 16:22:43

0

我花了2個多小時試圖弄清楚是怎麼回事,爲什麼我不能保存的Django的對象的字段分配從肥皂水數據結構值之後的對象。

由於@guillaumevincent提到Suds Text類是從unicode繼承而來的,並且實現不是100%正確的,所以Django在嘗試執行一些操作後失敗,這些操作將與基本unicode類型一起工作。

所以從問題的例子,我會做

customer_exists = Customer.objects.filter(email=unicode(customer.email))

而在我的情況下,我做了同樣

django_obj.field_name = suds_obj.field_name

希望這將節省一些時間的人:)

+0

unicode()未在python 3中定義。我的解決方案適用於python 2.7和python 3。 – 2014-03-28 14:25:17

相關問題