Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why codecs.iterdecode() eats empty strings?

Why the following two decoding methods return different results?

>>> 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'']

Is this a bug or expected behavior? My Python version 2.7.13.

like image 536
Cheng Lian Avatar asked May 11 '17 00:05

Cheng Lian


People also ask

What does codecs do in Python?

The codecs module defines a set of base classes which define the interface and can also be used to easily write your own codecs for use in Python. Each codec has to define four interfaces to make it usable as codec in Python: stateless encoder, stateless decoder, stream reader and stream writer.

What does decode () do in Python?

Python bytes decode() function is used to convert bytes to string object. Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is 'strict' meaning that encoding errors raise a UnicodeEncodeError.

What is encoding UTF-8 in Python?

UTF-8 is one of the most commonly used encodings, and Python often defaults to using it. UTF stands for “Unicode Transformation Format”, and the '8' means that 8-bit values are used in the encoding.

How do I decode a UTF-8 string in Python?

To decode a string encoded in UTF-8 format, we can use the decode() method specified on strings. This method accepts two arguments, encoding and error . encoding accepts the encoding of the string to be decoded, and error decides how to handle errors that arise during decoding.

How do you handle errors when encoding and decoding data?

This week's blog post is about handling errors when encoding and decoding data. You will learn 6 different ways to handle these errors, ranging from strictly requiring all data to be valid, to skipping over malformed data. Encode str objects into bytes objects.

What happens when a codec operation encounters malformed data?

When a codec operation encounters malformed data, that's an error: How can I deal with codec operation failures? Besides raising a UnicodeError exception, there are 5 other ways to deal with codec operation errors:

What is the result of encoding a Unicode string?

The result of encoding a unicode string is a str object. Given a sequence of encoded bytes as a str instance, the decode() method translates them to code points and returns the sequence as a unicode instance.

What are the functions of codecs and Io in Python?

With the codecs module functions and classes decode, encode , open, EncodedFile , iterencode , iterdecode , Codec.encode , Codec.decode , IncrementalEncoder , IncrementalDecoder , StreamWriter , Stream.Reader , StreamReaderWriter, and StreamRecoder. With the io module functions and classes open , and TextIOWrapper. In conclusion...


1 Answers

This is normal. iterdecode takes an iterator over encoded chunks and returns an iterator over decoded chunks, but it doesn't promise a one-to-one correspondence. All it guarantees is that the concatenation of all output chunks is a valid decoding of the concatenation of all input chunks.

If you look at the source code, you'll see it's explicitly discarding empty output chunks:

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

Be aware that the reason iterdecode exists, and the reason you wouldn't just call decode on all the chunks yourself, is that the decoding process is stateful. The UTF-8 encoded form of one character might be split over multiple chunks. Other codecs might have really weird stateful behavior, like maybe a byte sequence that inverts the case of all characters until you see that byte sequence again.

like image 109
user2357112 supports Monica Avatar answered Oct 21 '22 15:10

user2357112 supports Monica