溢出错误:当 > 时不支持的 UTF-8 序列长度编码字符串 [英] OverflowError: Unsupported UTF-8 sequence length when > encoding string

查看:34
本文介绍了溢出错误:当 > 时不支持的 UTF-8 序列长度编码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Twisted Resource 中,我返回一个 json 编码的 dict 作为下面的响应变量.数据是包含姓名、guid 和其他几个字段的 5 个人的列表,每个字段的长度少于 32 个字符,因此不是大量数据.

我经常收到此 OverflowError 异常,但我不太明白不受支持的 utf-8 序列长度指的是什么.

self.request.write(ujson.dumps(response))

<块引用>

exceptions.OverflowError: Unsupported UTF-8 sequence length when编码字符串

解决方案

如有疑问,请查看来源:http://code.google.com/p/rapidjson/source/browse/trunk/thirdparty/ultrajson/ultrajsonenc.c

当 UTF-8 长度为 5 或 6 个字节时会发生此错误.这个 JSON 实现没有实现.无论如何,如果您在浏览器中使用这些数据,这些字符将不起作用,因为它们超出了 UTF-16 的范围.

如果这真的经常发生,我会感到惊讶;它只会发生在 U+1FFFFF 上的 Unicode 代码点上,这非常罕见,并且由于超出此范围,大多数 Python 版本甚至不支持 Unicode 字符串.您应该找出为什么这些字符会出现在您的数据中.

Inside a Twisted Resource, I am returning a json encoded dict as the response var below. The data is a list of 5 people with name, guid, and a couple other fields less than 32 characters long each, so not a ton of data.

I get this OverflowError exception pretty often, but I don't quite understand what the unsupported utf-8 sequence length refers to.

self.request.write(ujson.dumps(response))

exceptions.OverflowError: Unsupported UTF-8 sequence length when encoding string

解决方案

When in doubt, check the source: http://code.google.com/p/rapidjson/source/browse/trunk/thirdparty/ultrajson/ultrajsonenc.c

This error happens when the UTF-8 length is 5 or 6 bytes. This JSON implementation doesn't implement that. Those characters won't work if you're using the data in a browser anyway, since they're outside the range of UTF-16.

I'd be surprised if this actually happened often; it'd only happen with Unicode codepoints over U+1FFFFF, which are vanishingly rare, and not even supported in Unicode strings by most builds of Python due to being outside this range. You should find out why these characters are showing up in your data.

这篇关于溢出错误:当 &gt; 时不支持的 UTF-8 序列长度编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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