Assume I read some content from socket in Python and have to decode it to UTF-8 on-the-fly.
I can not afford to keep all the content in memory, so I must decode it as I receive and save to file.
It can happen, that I will only receive partial bytes of character, (€-sign is represented by three bytes for example in Python as '\xe2\x82\xac').
Assume I have received only the first two bytes (\xe2\x82), if I try to decode it, I'm getting 'UnicodeDecodeError', as expected.
I could always try to decode the current content and check if it throws an Exception
Thanks
Guido's time machine strikes again.
>>> dec = codecs.getincrementaldecoder('utf-8')()
>>> dec.decode('foo\xe2\x82')
u'foo'
>>> dec.decode('\xac')
u'\u20ac'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With