简体   繁体   English

Python - 'ascii' 编解码器无法对位置 5 中的字符 u'\\xe9' 进行编码:序号不在范围内(128)

[英]Python - 'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128)

I'm trying to run a quick Django application that pulls data from Google AdWords and exposes the names of accounts that are managed by an agency.我正在尝试运行一个快速的 Django 应用程序,该应用程序从 Google AdWords 中提取数据并公开由代理机构管理的帐户名称。 When doing so, I get the following error:这样做时,我收到以下错误:

UnicodeEncodeError at /account-hierarchy/
'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128)

Here's the snippet:这是片段:

<table class="pretty-table">
  <thead>
    <tr>
      <td>Customer ID</td>
      <td>Client Name</td>
      <td>Can Manage Clients</td>
      <td>Account Currency</td>
    </tr>
  </thead>
  {% for account in managed_accounts %}
    <tr>
      {% for field in account %}
        <td>{{ field }}</td>
      {% endfor %}
    </tr>
  {% endfor %}
</table>

where the call to {{ field }} is the problematic line.其中对{{ field }}的调用是有问题的行。

I have already added我已经添加了

<meta http-equiv="content-type" content="text/html; charset=utf-8">

to the template I am rendering, but it still fails, so I believe the problem is not on the HTML template, but rather on the Python/Django engine.到我正在渲染的模板,但它仍然失败,所以我相信问题不在于 HTML 模板,而在于 Python/Django 引擎。

Any ideas how I can fix it?有什么想法可以解决吗?

Here's the View code that renders the template:这是呈现模板的视图代码:

def account_hierarchy(request):
manager_ids = settings.MANAGER_IDS
managed_accounts = []
for manager_id in manager_ids:
    managed_accounts.extend(adwords_utils.getManagedAccounts(manager_id))
return render_to_response('simple-table.html', {"managed_accounts": managed_accounts})

UPDATED Question更新的问题

  • Python Version: 2.7.6 Python 版本:2.7.6
  • Models.py is currently empty Models.py 当前为空

What's also curious is that if I remove this:同样奇怪的是,如果我删除它:

{% for field in account %}
    <td>{{ field }}</td>
  {% endfor %}

and I just print out the main array:我只是打印出主数组:

{{ managed_accounts }}

it works just fine.它工作得很好。 Not sure what's going on.不知道发生了什么。

Curious fact #2: As I managed to output the full array, I checked for character 'é' and I didn't find it on the final output.奇怪的事实 #2:当我设法输出完整数组时,我检查了字符 'é',但在最终输出中没有找到它。 Not sure where it was coming from.不知道它是从哪里来的。

The problem is likely that, in some place in your code, you accidentally defined a data structure to be a Python byte string, when you should have made it a Python Unicode string.问题很可能是,在您的代码中的某个地方,您不小心将数据结构定义为 Python 字节字符串,而您应该将其定义为 Python Unicode 字符串。 This leads Django and Python to convert from Django's Unicode string to your byte string, in a default way, using the ASCII codec.这导致 Django 和 Python 以默认方式使用 ASCII 编解码器从 Django 的 Unicode 字符串转换为您的字节字符串。

The error message gives some clues:错误消息提供了一些线索:

  • The error is perhaps occurring when you call account_hierarchy()当您调用account_hierarchy()时可能会发生错误
  • The phrase character u'\\xe9' in position 5: ordinal not in range(128) means that Python is trying to convert Unicode character 'é' (U+00E9) into an ASCII value from 0..127. character u'\\xe9' in position 5: ordinal not in range(128)的短语character u'\\xe9' in position 5: ordinal not in range(128)表示 Python 正在尝试将 Unicode 字符 'é' (U+00E9) 转换为 0..127 的 ASCII 值。 Look in your data for an 'é'.在您的数据中查找“é”。 Which field is it in?它在哪个领域?
  • The phrase 'ascii' codec means the conversion is likely inadvertent.短语'ascii' codec意味着转换可能是无意的。 If you intend to use UTF-8, you wouldn't have called the ASCII codec intentionally.如果您打算使用 UTF-8,您就不会故意调用 ASCII 编解码器。 But when you cause a conversion but don't specify a codec, Python uses ASCII.但是当您进行转换但未指定编解码器时,Python 使用 ASCII。
  • The word encode means conversion from Unicode to byte-oriented encoding. encode一词意味着从 Unicode 转换为面向字节的编码。 You have read Python's Unicode HOWTO article a couple of times, haven't you?你已经读过几次Python 的Unicode HOWTO文章,不是吗?

Function render_to_response() uses settings DEFAULT_CHARSET and DEFAULT_CONTENT_TYPE]( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CONTENT_TYPE ).函数render_to_response()使用设置DEFAULT_CHARSET和 DEFAULT_CONTENT_TYPE]( https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DEFAULT_CONTENT_TYPE )。 They should default to 'utf-8' and 'text/html', which should be appropriate for generating a UTF-8 encoded HTML page, but check.它们应该默认为“utf-8”和“text/html”,这应该适合生成 UTF-8 编码的 HTML 页面,但请检查。

I like the suggestion that you check your models.py to be sure that your models are defined in terms of Unicode data types, not byte string data types.我喜欢建议您检查models.py以确保您的模型是根据 Unicode 数据类型而不是字节字符串数据类型定义的。 Update : you say models.py is empty, so that won't be much help.更新:你说 models.py 是空的,所以不会有太大帮助。

Character handling, and Unicode vs byte strings, are handled differently in Python 2 and Python 3. Which version of Python are you using?字符处理以及 Unicode 与字节字符串在 Python 2 和 Python 3 中的处理方式不同。您使用的是哪个版本的 Python? Update : Python 2.7.6, thanks.更新:Python 2.7.6,谢谢。 The Unicode HOWTO I linked to above is for Python 2.7.x.我上面链接的Unicode HOWTO适用于 Python 2.7.x。

If you make sure your code handles strings as Unicode throughout, unless you really want a byte string, that will likely fix this problem.如果您确保您的代码始终将字符串作为 Unicode 处理,除非您真的想要一个字节字符串,这可能会解决这个问题。

Update : Consider modifying your template to give you debugging information.更新:考虑修改您的模板以提供调试信息。 Try something like these expressions, to see what's really in managed_accounts :尝试类似这些表达式,以查看managed_accounts的真正内容:

</thead>
<tr><td>managed_accounts is: {{ repr(managed_accounts) }}</tr></td>
{% for account in managed_accounts %}
  <tr>
  {% for field in account %}
    <td>{{ type(field) }}, {{ repr(field) }}</td>
  {% endfor %}
  </tr>
{% endfor %}

[ Updated in response to multiple updates from original poster.] [更新以响应原始海报的多次更新。]

your problem is same with mine,just encode question, you can add bellow code in your views.py of django project:你的问题和我的一样,只是编码问题,你可以在django项目的views.py中添加波纹管代码:

 1. #coding=utf-8  
 2. import sys
 3. reload(sys)
 4. sys.setdefaultencoding('utf-8')

my problem: Error during template rendering In template D:\\PythonProjects\\DjangoProject\\guest\\sign\\templates\\sign\\guest_manage.html, error at line 72 enter image description here我的问题:模板渲染时出错 在模板 D:\\PythonProjects\\DjangoProject\\guest\\sign\\templates\\sign\\guest_manage.html 中,第 72 行的错误在此处输入图像描述

这是属于 jinja2 模板的问题,将通过将编码设置为 utf-8 作为默认编码来解决。

sys.setdefaultencoding('utf-8')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\\xe9&#39; in position 54: ordinal not in range(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128) Cassandra:&#39;ascii&#39;编解码器无法在位置218处编码字符u&#39;\\ xe9&#39;:序数不在范围内(128) - Cassandra : 'ascii' codec can't encode character u'\xe9' in position 218: ordinal not in range(128) Python eyed3 UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置17编码字符u&#39;\\ xe9&#39;:序数不在范围内(128) - Python eyed3 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 17: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置31编码字符&#39;\\ xe9&#39;:安装金字塔期间序数不在range(128)中 - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 31: ordinal not in range(128) during installing pyramid UnicodeEncodeError:'ascii'编解码器无法编码位置17710中的字符u'\ xe7':序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128) Python:UnicodeEncodeError:&#39;ascii&#39; 编解码器无法在位置 0 中对字符 &#39;\Ο&#39; 进行编码:序号不在范围内 (128) - Python: UnicodeEncodeError: 'ascii' codec can't encode character '\u039f' in position 0: ordinal not in range(128) Python urllib&#39;ascii&#39;编解码器无法在位置5编码字符&#39;\\ u2757&#39;:序数不在范围内(128) - Python urllib 'ascii' codec can't encode character '\u2757' in position 5: ordinal not in range(128) Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置0编码字符u&#39;\\ xfc&#39;:序数不在范围内(128)-&gt; Excel - Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128) -> Excel Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法编码位置78中的字符u&#39;\\ xf1&#39;:序数不在范围内(128) - Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 78: ordinal not in range(128) python csv unicode'ascii'编解码器无法编码位置1中的字符u'\ xf6':序数不在范围内(128) - python csv unicode 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM