python - Unicode解码错误: 'utf8' codec can't decode byte 0xcb in position 5: invalid continuation byte

标签 python web.py

我的网络应用程序之前运行得很好,但几天前出现了问题,现在我可以启动我的网络应用程序,但是当我从本地(127.0.0.1)或远程(192.168.xxx.xxx)浏览我的网站时(仅只需打开主页,无需使用鼠标和键盘进行输入),使 Web 应用程序崩溃,如下所示:

Traceback (most recent call last):
File "/path/to/project/web/application.py", line 242, in process
  return self.handle()
File "/path/to/project/web/application.py", line 233, in handle
  return self._delegate(fn, self.fvars, args)
File "/path/to/project/web/application.py", line 415, in _delegate
  return handle_class(cls)
File "/path/to/project/web/application.py", line 390, in handle_class
  return tocall(*args)
File "./my_web_app.py", line 40, in GET
  simplejson.dumps(manus))
File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 286, in dumps
  return _default_encoder.encode(obj)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 226, in encode
  chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/dist-packages/simplejson/encoder.py", line 296, in iterencode
  return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcb in position 5: invalid continuation byte
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /" - 500 Internal Server Error
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /favicon.ico" - 404 Not Found
192.168.xxx.xxx:2131 - - [27/Nov/2013 16:51:09] "HTTP/1.1 GET /favicon.ico" - 404 Not Found

我不认为我的代码有什么问题,因为我的代码在我的计算机上运行得很好,只有当它在服务器上运行时才会出现错误。目录“web”是“web.py-0.34/web”的链接,它不是我的代码。

我的代码很简单:

urls = (
    '/', 'find_alternate',
    '/find_alternates', 'find_alternate',
    '/show_detail/(.+)', 'show_detail'
)
app = web.application(urls, globals())
class find_alternate:
    def GET(self):
        brands = [b.brandName for b in Brand.q.all()]
        brands.sort()
        manus = [oe.brandName for oe in OeNumber.q.group_by(OeNumber.brandName)]
        manus.sort()
        return render.find_alternates_main(simplejson.dumps(brands), simplejson.dumps(manus))
"""
some more functions, but not relevant
"""
render = web.template.render('/path/to/templates/')
web.template.Template.globals['str'] = str
if __name__ == "__main__":
    app.run()

我的创建表:

CREATE TABLE `brand` (
  `brandNo` int(11) NOT NULL,
  `brandName` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`brandNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

我现在的问题是如何将字符 Ë 从 Unicode 转换为 utf-8,以便 jsonsimple 可以解析它。在维基中我发现了这个:

Unicode: U+00CB
UTF-8: C3(hex) 8B(hex)

我是如何解决的: 将以下行添加到 my.cnf:

collation-server = utf8_unicode_ci
init_connect='SET NAMES utf8'
character-set-server = utf8
skip-character-set-client-handshake

将数据库转换为utf-8:

ALTER DATABASE `db_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

最佳答案

u'\xcb''\xc3\x8b' 的 unicode 表示形式,

>>> u'CITRO\xcbN'.encode('utf-8')
'CITRO\xc3\x8bN'

及其 latin-1 编码:

>>> u'CITRO\xcbN'.encode('latin-1')
'CITRO\xcbN'

所以你的服务器数据库似乎不是 utf-8 编码的。

我认为最好的解决方案是检查您的服务器表编码,如果不是 utf8,请迁移到 utf8。如果表采用 utf8 格式,则必须修复数据,因为数据不是。

或者,您可以从数据库设置推断编码并传递给 simplejson:

simplejson.dumps(manus, encoding=encoding)

但是这种方法会导致服务器和开发人员之间的差异以及将来的错误。

关于python - Unicode解码错误: 'utf8' codec can't decode byte 0xcb in position 5: invalid continuation byte,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20250634/

相关文章:

python - 如何使用 web.py 动态填充表单中的选择框/下拉框?

python - 如何通过 SSH 连接在 VPS 中运行的 Web 服务器运行其他程序?

python - Github api v3 通过 python oauth2 库访问 - 重定向问题

用于多图像 TIFF 的 Python PIL For Loop

python - 如何在Python中获取少于所需数量的位置参数?

python - 特征具有固定系数的多元线性回归

python - web.py Python 3 兼容吗?

python - 如何在 __main__ 中创建一个变量,供同一文件中定义的 web.py 类使用?

python - 如何在 LSTM/GRU 之上使用 keras 注意力层?

java - 如何读取 Elasticsearch 快照