为什么?编解码器.iterdecode()吃空弦?

2024-09-29 19:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么以下两种解码方法返回不同的结果?在

>>> import codecs
>>>
>>> data = ['', '', 'a', '']
>>> list(codecs.iterdecode(data, 'utf-8'))
[u'a']
>>> [codecs.decode(i, 'utf-8') for i in data]
[u'', u'', u'a', u'']

这是错误还是预期行为?我的Python版本2.7.13。在


Tags: 方法inimport版本fordata错误解码
1条回答
网友
1楼 · 发布于 2024-09-29 19:31:47

这很正常。iterdecode对编码块使用迭代器,并在解码块上返回迭代器,但它不保证一对一的对应关系。它保证所有输出块的连接是对所有输入块连接的有效解码。在

如果您查看source code,您将看到它显式地丢弃空的输出块:

def iterdecode(iterator, encoding, errors='strict', **kwargs):
    """
    Decoding iterator.
    Decodes the input strings from the iterator using an IncrementalDecoder.
    errors and kwargs are passed through to the IncrementalDecoder
    constructor.
    """
    decoder = getincrementaldecoder(encoding)(errors, **kwargs)
    for input in iterator:
        output = decoder.decode(input)
        if output:
            yield output
    output = decoder.decode("", True)
    if output:
        yield output

请注意,iterdecode存在的原因,以及您不会自己对所有块调用decode的原因是解码过程是有状态的。一个字符的UTF-8编码形式可能被分割成多个块。其他编解码器可能有非常奇怪的有状态行为,比如一个字节序列,它将所有字符的大小写颠倒,直到您再次看到该字节序列。在

相关问题 更多 >

    热门问题