python - 使用Django将Unicode字符存储到MySQL中 [英] python - Problem storing Unicode character to MySQL with Django

查看:239
本文介绍了python - 使用Django将Unicode字符存储到MySQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串

  uPlayed Mirror's Edge\\\™

哪些应该显示为

  Played Mirror's Edge™

但这是另一个问题。我的问题在于我将其放在一个模型中,然后尝试将其保存到数据库。 AKA:

  a = models.Achievement(name = uPlayed Mirror's Edge\\\™)
a。 save()

我得到:

 'ascii'编解码器不能将字符u'\\\™'编码在位置13:ordinal不在范围内(128)
/ pre>

完整堆栈跟踪(根据要求):

 追溯:
文件/var/home/ptarjan/django/mysite/django/core/handlers/base.pyin get_response
86. response = callback(request,* callback_args,** callback_kwargs)
文件/var/home/ptarjan/django/mysite/yourock/views/alias.pyin import_all
161. types.import_all(type,alias)
文件/ var / home /ptarjan/django/mysite/yourock/types/types.pyin import_all
52. return modules [type] .import_all(siteAlias,alias)
文件/ var / home / ptarjan / django / import_all
中的mysite / yourock / types / xbox.py117. achievementver = self.add_achievement(dict,si teAlias,别名)
文件/var/home/ptarjan/django/mysite/yourock/types/base_profile.pyin add_achievement
130. owner = siteAlias,
文件/ var / home / ptarjan / django / mysite / django / db / models / query.pyin get
304. num = len(clone)
文件/ var / home / ptarjan / django / mysite / django $ db
文件/ var / home / ptarjan / django / mysite / django / db /模型/ query.py在迭代器
275.对于self.query.results_iter()中的行:
文件/ var / home / ptarjan / django / mysite / django / db / models / sql / query.pyresult_iter
206.对于self.execute_sql(MULTI)中的行:
文件/var/home/ptarjan/django/mysite/django/db/models/sql/query.py in execute_sql
1734. cursor.execute(sql,params)
文件/var/home/ptarjan/django/mysite/django/db/backends/util.py中执行
19. return self.cursor.execute(sql,params)
文件/var/home/ptarjan/django/mysite/django/db/backends/mysql/base.py执行
83. return self.cursor.execute(query,args)
执行
中的文件/usr/lib/pymodules/python2.5/MySQLdb/cursors.py151. query = query%db.literal(args)
文件/ usr / lib / pymodules / python2.5 / MySQLdb / connections.pyin literal
247. return self.escape(o,self.encoders)
文件/usr/lib/pymodules/python2.5/MySQLdb/connections。 pyin string_literal
180. return db.string_literal(obj)

例外类型:UnicodeEncodeError at / import / xbox:bob
异常值:'ascii'将字符u'\\\™'编码在位置13:序号不在范围内(128)

模型的主要部分:

  class Achievement(MyBaseModel):
name = models.CharField(max_length = help_text =可读成就名)

我在我的settings.py

  DEFAULT_CHARSET ='utf-8'$ b $中使用了MySQL后端b  

所以基本上,我应该怎么处理所有这些unicode的东西?我希望如果我远离有趣的角色,并坚持使用UTF8,那么所有的只是工作。唉,似乎不是那么容易了。

解决方案

谢谢大家在这里发帖。这真的有助于我的unicode知识(而且其他人也学到了一些东西)。



我试图简化我的问题,提供所有信息。似乎我没有使用REALunicode字符串,而是使用BeautifulSoup.NavigableString,它们将自己替换为unicode字符串。所以所有的打印输出看起来像unicode,但是它们不是。



在MySQLDB库的某处,他们无法处理这些字符串。



这个工作:

 >>> Achievement.objects.get(name = uMirror's Edge\\\™)
< Achievement:Mirror's Edge™>

另一方面:

 >>> b = BeautifulSoup(u< span> Mirror's Edge\\\™< / span>)。span.string 
>>> Achievement.objects.get(name = b)
... Exceptoins ...
UnicodeEncodeError:'ascii'编解码器不能编码位置13中的字符u'\\\™'序号不在范围内(128)

但这样做:

 >>> Achievement.objects.get(name = unicode(b))
< Achievement:Mirror's Edge™>

所以,再次感谢所有的unicode帮助,我相信它会派上用场。但是现在...



警告:BeautifulSoup不返回 REAL Unicode字符串,应该被强制unicode()在做任何有意义的事情之前。


I have the string

 u"Played Mirror's Edge\u2122"

Which should be shown as

 Played Mirror's Edge™

But that is another issue. My problem at hand is that I'm putting it in a model and then trying to save it to a database. AKA:

a = models.Achievement(name=u"Played Mirror's Edge\u2122")
a.save()

And I'm getting :

'ascii' codec can't encode character u'\u2122' in position 13: ordinal not in range(128)

full stack trace (as requested) :

Traceback:
File "/var/home/ptarjan/django/mysite/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/var/home/ptarjan/django/mysite/yourock/views/alias.py" in import_all
  161.     types.import_all(type, alias)
File "/var/home/ptarjan/django/mysite/yourock/types/types.py" in import_all
  52.     return modules[type].import_all(siteAlias, alias)
File "/var/home/ptarjan/django/mysite/yourock/types/xbox.py" in import_all
  117.             achiever = self.add_achievement(dict, siteAlias, alias)
File "/var/home/ptarjan/django/mysite/yourock/types/base_profile.py" in add_achievement
  130.                 owner       = siteAlias,
File "/var/home/ptarjan/django/mysite/django/db/models/query.py" in get
  304.         num = len(clone)
File "/var/home/ptarjan/django/mysite/django/db/models/query.py" in __len__
  160.                 self._result_cache = list(self.iterator())
File "/var/home/ptarjan/django/mysite/django/db/models/query.py" in iterator
  275.         for row in self.query.results_iter():
File "/var/home/ptarjan/django/mysite/django/db/models/sql/query.py" in results_iter
  206.         for rows in self.execute_sql(MULTI):
File "/var/home/ptarjan/django/mysite/django/db/models/sql/query.py" in execute_sql
  1734.         cursor.execute(sql, params)
File "/var/home/ptarjan/django/mysite/django/db/backends/util.py" in execute
  19.             return self.cursor.execute(sql, params)
File "/var/home/ptarjan/django/mysite/django/db/backends/mysql/base.py" in execute
  83.             return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.5/MySQLdb/cursors.py" in execute
  151.             query = query % db.literal(args)
File "/usr/lib/pymodules/python2.5/MySQLdb/connections.py" in literal
  247.         return self.escape(o, self.encoders)
File "/usr/lib/pymodules/python2.5/MySQLdb/connections.py" in string_literal
  180.                 return db.string_literal(obj)

Exception Type: UnicodeEncodeError at /import/xbox:bob
Exception Value: 'ascii' codec can't encode character u'\u2122' in position 13: ordinal not in range(128)

And the pertinant part of the model :

class Achievement(MyBaseModel):
    name = models.CharField(max_length=100, help_text="A human readable achievement name")

I'm using a MySQL backend with this in my settings.py

DEFAULT_CHARSET = 'utf-8'

So basically, how the heck should I deal with all this unicode stuff? I was hoping it would all "just work" if I stayed away from funny character sets and stuck to UTF8. Alas, it seems to not be just that easy.

解决方案

Thank you to everyone who was posting here. It really helps my unicode knowledge (and hoepfully other people learned something).

We seemed to be all barking up the wrong tree since I tried to simplify my problem and didn't give ALL information. It seems that I wasn't using "REAL" unicode strings, but rather BeautifulSoup.NavigableString which repr themselves as unicode strings. So all the printouts looked like unicode, but they weren't.

Somewhere deep in the MySQLDB library they couldn't deal with these strings.

This worked :

>>> Achievement.objects.get(name = u"Mirror's Edge\u2122")
<Achievement: Mirror's Edge™>

On the other hand :

>>> b = BeautifulSoup(u"<span>Mirror's Edge\u2122</span>").span.string
>>> Achievement.objects.get(name = b)
... Exceptoins ...
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 13: ordinal not in range(128)

But this works :

>>> Achievement.objects.get(name = unicode(b))
<Achievement: Mirror's Edge™>

So, thanks again for all the unicode help, I'm sure it will come in handy. But for now ...

WARNING : BeautifulSoup doesn't return REAL unicode strings and should be coerced with unicode() before doing anything meaningful with them.

这篇关于python - 使用Django将Unicode字符存储到MySQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆