python - 在 django 和 unicode 中使用 python 登录时出现问题

标签 python django logging unicode decoding

现在完全糊涂了...我正在用 python/django 开发并使用 python 日志记录。我所有的应用程序都需要 unicode,而我的所有模型都只有一个 unicode()`, return u'..' 实现的方法。现在,当我记录日志时,我遇到了一个非常奇怪的问题,我花了很长时间才发现我可以重现它。我已经尝试过 Py 2.5.5 和 Py 2.6.4 以及同样的东西。所以

每当我做一些直接的日志记录时,比如:

logging.debug(u'new value %s' % group) 

这调用模型组。unicode(): return unicode(group.name)

我的 unicode 方法都是这样的:

def __unicode__(self):
    return u'%s - %s (%s)' % (self.group, self.user.get_full_name(), self.role)

即使 group.name 是 XXX 或 ÄÄÄ(需要 unicode)也能正常工作。但是当我出于某种原因想要记录一个集合、列表、字典、django 查询集和单个实例时,例如该列表可能是 unicode 或不是我遇到麻烦...

所以每当 group.name 需要像 Luleå(我的家乡)这样的 unicode 时,这就会给我一个 UnicodeDecodingError

logging.debug(u'new groups %s' % list_of_groups)

通常我会收到这样的错误:

Exception Type:     UnicodeDecodeError
Exception Value:    ('ascii',  '<RBACInstanceRoleSet: s2 | \xc3\x84\xc3\x96\xc3\x96\xc3\x85\xc3\x85\xc3\x85 Gruppen>]', 106, 107, 'ordinal not in range(128)')

但是如果我执行 print list_of_groups 一切都会在终端上正常显示

因此,我的理解是列表开始生成自身并对它的所有元素执行 repr() 并且它们返回它们的值 - 在这种情况下它应该是 's2 | ÅÄÖÖ',然后列表将自身显示为(ascii,列表中的内容),然后在尝试将 ascii 解码为 un​​icode 时,这当然不起作用 - 因为其中之一当对其进行 repr 时,列表中的元素返回了一个 u'...' 。

但这是为什么????´

为什么每当我记录 group.name 和 so 或 group 之类的简单内容并调用 unicode 方法时,为什么一切正常并且 unicode/ascii 得到正确处理。每当我变得懒惰并想记录一个列表时,只要遇到 unicode 字符,设置或其他事情就会变坏......

一些成功和失败的例子。如果 group.name 我转到模型字段并且 group 调用 __unicode__()

    logging.debug("1. group: %s " % group.name) # WORKS
    logging.debug(u"2. group: %s " % group) # WORKS
    logging.debug("3. group: %s " % group) # FAILS
    logging.debug(u"4. group: %s " % group.name) # WORKS
    logging.debug("5. group: %s " % group.name) # WORKS

...我真的以为我掌握了 Unicode ;-(

最佳答案

这是我的测试代码:

#-*- coding: utf-8 -*-                                      
class Wrap:                                          
    def __init__(self, s): self.s = s
    def __repr__(self): return repr(self.s)    
    def __unicode__(self): return unicode(self.s)
    def __str__(self): return str(self.s)

s = 'hello'  # a plaintext string
u = 'ÅÄÖÖ'.decode('utf-8') 
l = [s,u]
test0 = unicode(repr(l))
test1 = 'string %s' % l
test2 = u'unicode %s' % l

当你运行上面的代码时,它运行良好。但是,如果您将 repr 的声明更改为: def repr(self): 返回 unicode(self.s)

然后它中止:

Traceback (most recent call last):
  File "mytest.py", line 13, in <module> unicode(l)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3:
   ordinal not in range(128)

所以看起来对象层次结构中的某个人有一个 repr() 实现,它错误地返回一个 unicode 字符串而不是一个普通的字符串。正如其他人提到的那样,当您执行类似

的格式字符串时
'format %s' % mylist

而 mylist 是一个序列,python 会自动对其调用 repr() 而不是 unicode() (因为没有“正确”的方式将列表表示为 unicode 字符串)。

这里可能是 django 出了问题,或者您可能在您的某个模型中错误地实现了 __repr__

#

关于python - 在 django 和 unicode 中使用 python 登录时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2111765/

相关文章:

html - Django - 使用没有静态文件的 css 显示 html

Django:使用 Django ORM 实现 JOIN?

node.js - 错误 : ENOENT with Bunyan rotating-file logging (NodeJS)

mongodb - 如何减少 docker-compose 图像中的 mongo 日志详细程度?

python - python : How to install missing dependency?

python - 如何使用 Python 在二进制文件中查找 float ?

python - 如何使用 Homebrew 软件在 Cassandra 上设置 super 用户帐户?

python - django.core.exceptions.ImproperlyConfigured : Error loading MySQLdb module: No module named 'MySQLdb'

node.js - 根据条件在react js中导出?

python - 将连续的整数组合在一起