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
对编码块使用迭代器,并在解码块上返回迭代器,但它不保证一对一的对应关系。它保证所有输出块的连接是对所有输入块连接的有效解码。在如果您查看source code,您将看到它显式地丢弃空的输出块:
请注意,
iterdecode
存在的原因,以及您不会自己对所有块调用decode
的原因是解码过程是有状态的。一个字符的UTF-8编码形式可能被分割成多个块。其他编解码器可能有非常奇怪的有状态行为,比如一个字节序列,它将所有字符的大小写颠倒,直到您再次看到该字节序列。在相关问题 更多 >
编程相关推荐