On Tue, May 25, 2021 at 8:21 PM Cameron Simpson <c...@cskk.id.au> wrote: > When you go: > > text = str(data) > > that is _assuming_ a particular text encoding stored in the data. You > really ought to specify an encoding here. If you've not specified the > CHARSET for things, 'ascii' would be a conservative choice. The IMAP RFC > talks about what to expect in section 4 (Data Formats). There's quite a > lot of possible response formats and I can understand imaplib not > getting deeply into decoding these.
Worse than that: what you actually get is the repr of the bytes. That might happen to look a lot like an ASCII decode, but if the string contains unprintable characters, quotes, or anything outside of the ASCII range, it's going to represent it as an escape code. The best way to turn bytes into text is the decode method: data.decode("UTF-8") ChrisA -- https://mail.python.org/mailman/listinfo/python-list