- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是处理 UTF-8 时的一个常见错误 - “无效标记”
在我的示例中,它来自于处理不尊重 unicode 字符的 SOAP 服务提供者,只是将值截断为 100 个字节并忽略第 100 个字节可能位于多字节字符的中间:例如:
<name xsi:type="xsd:string">浙江家庭教会五十人遭驱散及抓打 圣诞节聚会被断电及抢走物品(图、视频\xef\xbc</name>
最后两个字节是 3 字节 unicode 字符的剩余部分,在截断刀假定世界使用 1 字节字符之后。下一站,sax 解析器和:
xml.sax._exceptions.SAXParseException: <unknown>:1:2392: not well-formed (invalid token)
我不再关心这个角色了。它应该从文档中删除并允许 sax 解析器运行。
除了这些值之外,XML 回复在所有其他方面都是有效的。
问题:如何在不解析整个文档并重新发明 UTF-8 编码来检查每个字节的情况下删除这个字符?
使用:Python+SUDS
最佳答案
事实证明,SUDS 将 xml 视为类型“字符串”(不是 unicode),因此这些是编码值。
1) 过滤器:
badXML = "your bad utf-8 xml here" #(type <str>)
#Turn it into a python unicode string - ignore errors, kick out bad unicode
decoded = badXML.decode('utf-8', errors='ignore') #(type <unicode>)
#turn it back into a string, using utf-8 encoding.
goodXML = decoded.encode('utf-8') #(type <str>)
2) SUDS:见https://fedorahosted.org/suds/wiki/Documentation#MessagePlugin
from suds.plugin import MessagePlugin
class UnicodeFilter(MessagePlugin):
def received(self, context):
decoded = context.reply.decode('utf-8', errors='ignore')
reencoded = decoded.encode('utf-8')
context.reply = reencoded
和
from suds.client import Client
client = Client(WSDL_url, plugins=[UnicodeFilter()])
希望这对某人有帮助。
注意:感谢John Machin !
参见:Why is python decode replacing more than the invalid bytes from an encoded string?
python issue8271关于 errors='ignore'
可以在这里妨碍你。如果没有在 python 中修复此错误,'ignore' 将消耗接下来的几个字节以满足长度
during the decoding of an invalid UTF-8 byte sequence, only the
start byte and the continuation byte(s) are now considered invalid, instead of the number of bytes specified by the start byte
问题已修复:
Python 2.6.6 rc1
Python 2.7.1 rc1(以及 2.7 的所有 future 版本)
Python 3.1.3 rc1(以及 3.x 的所有 future 版本)
Python 2.5 及以下版本将包含此问题。
在上面的例子中,"\xef\xbc</name".decode('utf-8', errors='ignore')
应该
返回"</name"
,但在“有漏洞”的 python 版本中,它返回 "/name"
.
前四位 ( 0xe
) 描述了一个 3 字节的 UTF 字符,所以字节 0xef
, 0xbc
,然后(错误地)0x3c
( '<'
) 被消耗。
0x3c
不是一个有效的连续字节,它首先创建了无效的 3 字节 UTF 字符。
python 的固定版本仅删除第一个字节和仅有效的后续 字节,留下0x3c
未消费
关于Python UTF-8 XML 解析 (SUDS) : Removing 'invalid token' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719330/
UTF-8、UTF-16 和 UTF-32 之间有何区别? 据我所知,它们都将存储 Unicode,并且每个都使用不同数量的字节来表示字符。选择其中之一是否有优势? 最佳答案 当 ASCII 字符代表
好的。我知道这看起来像典型的“他为什么不直接用谷歌搜索或去 www.unicode.org 查一下?”问题,但对于这样一个简单的问题,在检查了两个来源后,我仍然无法回答。 我很确定这三种编码系统都支持
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
UTF-16 是一种双字节字符编码。交换两个字节的地址将产生 UTF-16BE 和 UTF-16LE。 但我发现在 Ubuntu gedit 文本编辑器中存在名称 UTF-16 编码,以及 UTF-1
我想将 UTF-16 字符串转换为 UTF-8。我通过 Unicode 发现了 ICU 库。我在转换时遇到问题,因为默认设置是 UTF-16。我试过使用转换器: UErrorCode myError
UTF-16 需要 2 个字节,UTF-8 需要 1 个字节。 而USB是面向8bit的,UTF-8更自然。 UTF-8 向后兼容 ASCII,而 UTF-16 则不然。 UTF-16 需要 2 个字
我对将 unicode 字符转换为十六进制值有点困惑。 我正在使用这个网站获取字符的十六进制值。 ( https://www.branah.com/unicode-converter ) 如果我输入“
我已经用UTF-8编码创建了一个文件,但是我不了解其在磁盘上占用的大小的规则。这是我的完整研究: 首先,我创建了一个带有印地语字母“'”的文件,Windows 7上的文件大小为 8个字节。 现在带有两
如何将WideString(或其他长字符串)转换为UTF-8中的字节数组? 最佳答案 这样的功能将满足您的需求: function UTF8Bytes(const s: UTF8String): TB
我有一个奇怪的验证程序,用于验证utf-8字符串是否是有效的主机名(PHP中的Zend Framework主机名valdiator)。它允许IDN(国际化域名)。它将比较每个子域与由其十六进制字节表示
在 utf16 和 utf32 中,一个字节的零是否意味着空?就像在 utf8 中一样,还是我们需要 2 个和 4 个字节的零来相应地在 utf16 和 utf32 中创建 null? 最佳答案 在
这是基于我的观察,对于 mysql,默认字符集 utf8 有点误导,它不支持完整的 Unicode,因为它无法存储四字节 UTF-8 编码的字符。它实际上是 utf8mb4 字符集,它是完整的 Uni
我只有处理 ASCII(单字节字符)的经验,并且阅读了很多关于人们如何以不同方式处理 Unicode 的帖子,这些帖子提出了他们自己的一系列问题。 此时我对 Unicode 的了解非常有限,我读到过U
我明白 std::codecvt在 C++11 中执行 UTF-16 和 UTF-8 之间的转换,并且 std::codecvt执行 UTF-32 和 UTF-8 之间的转换。是否可以在 UTF-8
我正在编写一个 HTTP 服务器并使用 trivial-utf-8:write-utf-8-bytes 来响应请求。我听说Babel就像trivial-utf-8但效率更高,所以我想试一试。搜索了一段
我正在设计一个新的 CMS,但想要设计它来满足我 future 的所有需求,比如多语言内容,所以我认为 Unicode (UTF-8) 是最好的解决方案 但是通过一些搜索我得到了这篇文章 http:/
例如,假设我在字符串中有以下 xml: 如果我尝试将其插入到带有 Xml 列的 SQL Server 2005 数据库表中,我将收到以下错误(我使用的是 EF 4.1,但我认为这无关紧要): XM
我正在使用 Python CSV 库读取两个 CSV 文件。 一种使用 UTF-8-BOM 编码,另一种使用 UTF-8 编码。在我的实践中,我发现使用“utf-8-sig”作为编码类型可以读取这两个
假设我的数据库设置如下以使用 utf-8(mysql 中的完整 4mb 版本) mysql_query("SET CHARACTER SET utf8mb4"); mysql_query("SET N
我是一名优秀的程序员,十分优秀!