與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'))
[]
我很感謝任何提示..
嗨,你可以嘗試unicode(customer.email,'iso8859-1'),讓我知道它是否有幫助... – Jingo 2012-02-10 22:24:06
Th但它沒有: 如果不是的話。Customer.objects.filter(email = unicode(customer.email,'iso8859-1')): TypeError:解碼不支持Unicode – 2012-02-11 07:44:08
你能解決這個問題嗎?看起來像我面臨着同樣的問題 – Fedor 2014-03-26 09:19:32