Unicodedecodeerror:' ascii'编解码器可以在位置1中解码字节0xc4:序数不在范围内(128)
因此,正如问题的标题所说,我在编码/解码字符串的问题上有问题。
我正在使用: Python 2.7 | Django 1.11 | jinja2 2.8
基本上,我正在从数据库中检索一些数据,我将其序列化,在其上设置高速缓存,然后获取缓存,对其进行测试并将其渲染到模板。
问题:
我有名字中具有“”之类的人物的人的名字和姓氏。 我使用json.dumps序列化。
一个序列化词典的示例看起来像(我有10个这样):
active_agents = User.region_objects.get_active_agents()
agents_by_commission_last_month = active_agents.values(....
"first_name", "last_name").order_by(
'-total_paid_transaction_value_last_month')
我会喜欢:
for key, value in context.items():
......
value = json.dumps(list(value), default=str, ensure_ascii=False).encode('utf-8')
然后,当我设置高速缓存时, 上述代码和密钥是region_agents_by_commess_last_month
(如上一个代码中的变量)
,我必须获取缓存。因此,我正在执行相同的过程,但逆转了。
serialized_keys = ['agencies_by_commission_last_month',
'region_agents_by_commission_last_month', 'region_agents_by_commission_last_12_months',
'region_agents_by_commission_last_30_days',
'agencies_by_commission_last_year',
'agencies_by_commission_last_12_months',
'agencies_by_commission_last_30_days',
'region_agents_by_commission_last_year',
'agency',
'for_agent']
context = {}
for key, value in region_ranking_cache.items():
if key in serialized_keys:
objects = json.loads(value, object_hook=_decode_dict)
for serilized_dict in objects:
....
d['full_name'] = d['first_name'] + " " + d['last_name']
full_name = d['full_name'].decode('utf-8').encode('utf-8')
d['full_name'] = full_name
print(d['full_name'])
....
其中_decode_dict的object_hook看起来像:
print的结果:cătălinpintea,没关系。 但是在字典中,我渲染:'full_name':'c \ xc4 \ x83t \ xc4 \ xc4 \ x83lin pintea',
def _decode_list(data):
rv = []
for item in data:
if isinstance(item, unicode):
item = item.encode('utf-8')
elif isinstance(item, list):
item = _decode_list(item)
elif isinstance(item, dict):
item = _decode_dict(item)
rv.append(item)
return rv
def _decode_dict(data):
rv = {}
for key, value in data.items():
if isinstance(key, unicode):
key = key.encode('utf-8')
if isinstance(value, unicode):
value = value.encode('utf-8')
elif isinstance(value, list):
value = _decode_list(value)
elif isinstance(value, dict):
value = _decode_dict(value)
rv[key] = value
return rv
基本上,我使用此对象挂钩函数来eNcode()concode()当json.loads
时,键和值。
这就是我避免在views.py
中避免此错误的方式。
错误
在模板上的某个地方,我正在使用:
< td> {{agent.full_name}}</td>
and agent.full_name来自:'
' full_name':'C \ xc4 \ x83t \ xc4 \ xc4 \ x83lin pintea',
trackback
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in inner
185. return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/app/crmrebs/utils/__init__.py" in wrapper
255. return http_response_class(t.render(output, request))
File "/usr/local/lib/python2.7/dist-packages/django_jinja/backend.py" in render
106. return mark_safe(self.template.render(context))
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py" in render
989. return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py" in handle_exception
754. reraise(exc_type, exc_value, tb)
File "/app/crmrebs/jinja2/ranking/dashboard_ranking.html" in top-level template code
1. {% extends "base.html" %}
File "/app/crmrebs/jinja2/base.html" in top-level template code
1. {% extends "base_stripped.html" %}
File "/app/crmrebs/jinja2/base_stripped.html" in top-level template code
94. {% block content %}
File "/app/crmrebs/jinja2/ranking/dashboard_ranking.html" in block "content"
83. {% include "dashboard/region_ranking.html" %}
File "/app/crmrebs/jinja2/dashboard/region_ranking.html" in top-level template code
41. {% include "dashboard/_agent_ranking_row_month.html" %}
File "/app/crmrebs/jinja2/dashboard/_agent_ranking_row_month.html" in top-level template code
2. <td>{{ agent.full_name }}</td>
Exception Type: UnicodeDecodeError at /ranking
Exception Value: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128)
,这是从错误到达的地方。我尝试了其他事情,但我想这是Python 2.7的限制。我通常使用Python 3.9,但是对于此项目,我必须使用2.7。 我在这里尝试了其他答案,但没有任何帮助。
有人可以帮助我正确地序列化该字典,我该如何避免这种混乱?
我希望我能使自己清楚。
祝大家有美好的一天!
So, as title of the questions says, I have a problem with encoding/decoding of strings.
I am using:
python 2.7 | django 1.11 | jinja2 2.8
Basically, I am retrieving some data from data base, I serialize it, set cache on it, then get the cache, deserialize it and rendering it to the template.
Problem:
I have first names and last names of persons that have characters like "ă" in the names.
I serialize using json.dumps.
A sample of serialized dictionary looks like (I have 10 like this):
active_agents = User.region_objects.get_active_agents()
agents_by_commission_last_month = active_agents.values(....
"first_name", "last_name").order_by(
'-total_paid_transaction_value_last_month')
Then, when I set the cache I do it like:
for key, value in context.items():
......
value = json.dumps(list(value), default=str, ensure_ascii=False).encode('utf-8')
, where value is the list of dictionaries returned by .values()
from the aforementioned code and key is region_agents_by_commission_last_month
(like the variable from the previous code)
Now, I have to get the cache. So I am doing the same process, but reversed.
serialized_keys = ['agencies_by_commission_last_month',
'region_agents_by_commission_last_month', 'region_agents_by_commission_last_12_months',
'region_agents_by_commission_last_30_days',
'agencies_by_commission_last_year',
'agencies_by_commission_last_12_months',
'agencies_by_commission_last_30_days',
'region_agents_by_commission_last_year',
'agency',
'for_agent']
context = {}
for key, value in region_ranking_cache.items():
if key in serialized_keys:
objects = json.loads(value, object_hook=_decode_dict)
for serilized_dict in objects:
....
d['full_name'] = d['first_name'] + " " + d['last_name']
full_name = d['full_name'].decode('utf-8').encode('utf-8')
d['full_name'] = full_name
print(d['full_name'])
....
where _decode_dict for object_hook looks like:
The result from print: Cătălin Pintea , which is ok.
But in the dictionary I render: 'full_name': 'C\xc4\x83t\xc4\x83lin Pintea',
def _decode_list(data):
rv = []
for item in data:
if isinstance(item, unicode):
item = item.encode('utf-8')
elif isinstance(item, list):
item = _decode_list(item)
elif isinstance(item, dict):
item = _decode_dict(item)
rv.append(item)
return rv
def _decode_dict(data):
rv = {}
for key, value in data.items():
if isinstance(key, unicode):
key = key.encode('utf-8')
if isinstance(value, unicode):
value = value.encode('utf-8')
elif isinstance(value, list):
value = _decode_list(value)
elif isinstance(value, dict):
value = _decode_dict(value)
rv[key] = value
return rv
Basically, I use this object hook function in order to encode() to utf-8 all keys and value when json.loads
.
This is how I avoided this error to be thrown in views.py
.
Error
Somewhere on template, I am using:
<td>{{ agent.full_name }}</td>
And agent.full_name comes from : 'full_name': 'C\xc4\x83t\xc4\x83lin Pintea',
Traceback
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in inner
185. return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/app/crmrebs/utils/__init__.py" in wrapper
255. return http_response_class(t.render(output, request))
File "/usr/local/lib/python2.7/dist-packages/django_jinja/backend.py" in render
106. return mark_safe(self.template.render(context))
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py" in render
989. return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py" in handle_exception
754. reraise(exc_type, exc_value, tb)
File "/app/crmrebs/jinja2/ranking/dashboard_ranking.html" in top-level template code
1. {% extends "base.html" %}
File "/app/crmrebs/jinja2/base.html" in top-level template code
1. {% extends "base_stripped.html" %}
File "/app/crmrebs/jinja2/base_stripped.html" in top-level template code
94. {% block content %}
File "/app/crmrebs/jinja2/ranking/dashboard_ranking.html" in block "content"
83. {% include "dashboard/region_ranking.html" %}
File "/app/crmrebs/jinja2/dashboard/region_ranking.html" in top-level template code
41. {% include "dashboard/_agent_ranking_row_month.html" %}
File "/app/crmrebs/jinja2/dashboard/_agent_ranking_row_month.html" in top-level template code
2. <td>{{ agent.full_name }}</td>
Exception Type: UnicodeDecodeError at /ranking
Exception Value: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128)
And this is from where the error comes. I tried other things, but I guess it is a limitation of python 2.7. I usually use python 3.9, but for this project I have to use 2.7.
I tried other answers around here but nothing really helped.
Can anybody help me to serialize this dictionary properly and how can I avoid this mess?
I hope I made myself clear.
Have a nice day everyone !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我设法解决了我的问题。
active_agents.values(....“ first_name”,“ last_name”)。
检索了一个词典,其中其键和值已经在Unicode中(bacause在模型中配置的方式。序列化很好。
的确,到达模板的最终结果看起来像
'C \ XC4 \ X83T \ XC4 \ x83lin'
。错误来自 /XC4 /。{{agent.full_name.decode(“ utf-8”)}}} ,它给了我正确的结果:
cătălinPintea
谢谢@boargules。的确,
d ['last_name']
和d ['first_name']
在Unicode中。因此,当我进行串联时,我必须添加u“”
。So, I managed to solve my issue.
active_agents.values(...."first_name", "last_name").order_by('-total_paid_transaction_value_last_month')
retrieved a dictionary where its key and values were already in unicode (bacause of the way it was configured in models.py, django 1.11 and python2.7. So, the process of serializing was just fine.
It is indeed true that the final result that went to template was looking like
’C\xc4\x83t\xc4\x83lin'
. The error came from /xc4/.{{ agent.full_name.decode("utf-8") }}, which gave me the right result:
Cătălin Pintea
Thanks @BoarGules. It was true that
d['last_name']
andd['first_name']
were in unicode. So when I did the concatenation, I had to addu" "
.