简体   繁体   English

Python 2.7 + Django 1.7 + PostgreSQL 9.3:尝试将一些文本保存到数据库时出现UnicodeEncodeError。 是什么赋予了?

[英]Python 2.7 + Django 1.7 + PostgreSQL 9.3: I'm getting a UnicodeEncodeError when trying to save some text to my database. What gives?

In my models.py file I have defined a model roughly as follows: 在我的models.py文件中,我大致定义了一个模型,如下所示:

class MyThing(models.Model):
    thing_id     = models.CharField(max_length=20, unique=True, null=False)
    code_snippet = models.TextField(null=True)

I am trying to populate this table with a bunch of data that I am pulling down from a Google API. 我正在尝试使用从Google API中提取的一堆数据填充该表。 One piece is a snippet of JavaScript. 其中一小段是JavaScript。 One of the items being returned from this API has a snippet of JS that is throwing an error. 从此API返回的项目之一包含JS片段,该片段引发错误。

The error I am getting is: 我得到的错误是:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 213-214: ordinal not in range(128) UnicodeEncodeError:“ ascii”编解码器无法编码位置213-214中的字符:序数不在范围内(128)

The offending bit of text appears in this section: 令人反感的文字出现在本节中:

type="text/javascript" charset="utf-\xad\u20108"></script>

I'm almost certain it is the \\xad, which must be some sort of unicode escape sequence. 我几乎可以肯定它是\\ xad,它必须是某种unicode转义序列。

I checked my PostgreSQL encoding, and it is using UTF-8. 我检查了PostgreSQL编码,它使用的是UTF-8。

I also place this line at the top of my models.py, but it didn't make any difference: 我还将这一行放置在我的models.py的顶部,但没有任何区别:

from __future__ import unicode_literals

What exactly is going on here? 这到底是怎么回事? Why can't my unicode-aware database save this string? 为什么我的支持unicode的数据库无法保存此字符串? Also, when did I start using an 'ascii' codec ? 此外,我何时开始使用'ascii' codec

---- EDIT: full stack trace below ----- ----编辑:下面的完整堆栈跟踪-----

ERROR/MainProcess] Task apps.r2d_service.tasks.sync_jsnippets_task[e74712bf-2e04-4bed-b08c-f24f9ebb3049] raised unexpected: UnicodeEncodeError('ascii', u'<script type="text/javascript">\n    window._sm_plcmnt = "finyo_5225_5119";\n    var _sm_viewed    = false;\n</script>\n<script src="https://cdn.company412media.com/ng/js/augur.js" type="text/javascript" charset="utf-\xad\u20108"></script>\n<script src=\'https://cdn.company412media.com/ng/pub.js\'></script>\n<script type="text/javascript">\n$$(\'#bib_actions_table tr:nth-child(1) a\').each(function (tab) {\n  tab.observe(\'click\', function (e, el) {\n    if (!_sm_viewed) {\n      SM_Augur.init();\n      _sm_viewed = true;\n    }\n  });\n});\n\n</script>\n', 213, 215, 'ordinal not in range(128)')
Traceback (most recent call last):
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/celery/app/trace.py", line 437, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/vagrant/myapp/apps/r2d_service/tasks.py", line 25, in sync_jsnippets_task
    sync_jsnippets()
  File "/home/vagrant/myapp/apps/r2d_service/sync.py", line 44, in sync_jsnippets
    myapp_creative.sync(r2d_creative)
  File "/home/vagrant/myapp/apps/r2d_service/models.py", line 244, in sync
    self.save()
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 590, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 618, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 680, in _save_table
    forced_update)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/base.py", line 724, in _do_update
    return filtered._update(values) > 0
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 90, in execute
    logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),
UnicodeEncodeError: 'ascii' codec can't encode characters in position 213-214: ordinal not in range(128)

You need to convert those strings using base64 then you can save it to database. 您需要使用base64转换这些字符串,然后可以将其保存到数据库。

import base64

base64.b64encode("stings")

暂无
暂无

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

相关问题 应用程序(Python Django,PostgreSql)已成功部署在Heroku上,但是尝试打开时出现错误 - App (Python Django, PostgreSql) is deployed successfully on Heroku, but I'm getting error when trying to open Django尝试将值保存到数据库,但当我尝试保存时值变成NULL - Django trying to save value to database but value turns into NULL when i'm trying to save python:尝试解析列表时获取UnicodeEncodeError - python: getting UnicodeEncodeError when trying to parse a list 我正在尝试在虚拟环境中安装 django 但出现一些错误,我不明白它是怎么回事 - I'm trying to install django in virtual environment but getting some error and I don't understand what is it about 编写简单的查询,获取unicodeEncodeError,python 2.7 - Writing a simple query, getting a unicodeEncodeError, python 2.7 使用 python,我试图从数据库访问文本,但是当我只打印与 foreach 循环时,我得到了不同的结果 - Using python, I'm trying to access text from a database, but I'm getting different results when I do just print vs a foreach loop 尝试从请求文本中读取 JSON 字符串时出现 UnicodeEncodeError - Getting UnicodeEncodeError when trying to read a JSON string from a request text 适用于python 2.7和Django 1.7的OpenShift HTTPS - OpenShift HTTPS for python 2.7 and django 1.7 使用Django 1.7迁移的Python 2.7未绑定方法 - Python 2.7 unbound method with Django 1.7 migrations UnicodeEncodeError Python 2.7 - UnicodeEncodeError Python 2.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM