【问题标题】:Base.py: Unicode equal comparison failed to convert both arguments to Unicode inBase.py:Unicode 相等比较未能将两个参数转换为 Unicode
【发布时间】:2016-08-05 10:37:34
【问题描述】:

我正在查看几个处理相同问题的相关问题,但我仍然找不到解决方法。

事实证明,每次我执行一个与 Django 相关的命令时,它都会打印出预期的输出以及如下内容:

/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py:307: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
return name == ":memory:" or "mode=memory" in force_text(name)

以下是该行的上下文:

def is_in_memory_db(self, name):
    return name == ":memory:" or "mode=memory" in force_text(name)

尽管 Django 服务器工作正常,但总是在我的屏幕上打印出这条消息有点烦人。那么,为什么会发生这种情况以及如何解决呢?

【问题讨论】:

标签: python django unicode


【解决方案1】:

使用decode('utf-8')进行正确比较:

name.decode('utf-8') == ":memory:" or "mode=memory" in force_text(name)

使用完整信息:

Unicode HOWTO

Solving Unicode Problems in Python 2.7

【讨论】:

  • 使用 decode() 而不是 encode() 它可以工作。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
相关资源
最近更新 更多