Python 3 how to convert a list of bytes objects to a list of strings?

Chris Green cl at isbd.net
Sun Aug 30 04:00:59 EDT 2020


Cameron Simpson <cs at cskk.id.au> wrote:
> On 29Aug2020 16:50, Chris Green <cl at isbd.net> wrote:
> >However the problem appears to be that internally in Python 3 mailbox
> >class there is an assumption that it's being given 'ascii'.  Here's
> >the error (and I'm doing no processing of the message at all):-
> >
> >    Traceback (most recent call last):
> >      File "/home/chris/.mutt/bin/filter.py", line 102, in <module>
> >        mailLib.deliverMboxMsg(dest, msg, log)
> >      File "/home/chris/.mutt/bin/mailLib.py", line 52, in deliverMboxMsg
> >        mbx.add(msg)
> [...]
> 
> Here is the entire save-to-mbox code form my own mailfiler:
> 
>     text = M.as_string(True).replace('\nFrom ', '\n>From ')
>     with open(folderpath, "a") as mboxfp:
>           mboxfp.write(text)
> 
> where M is the current message, a Message object.
> 
> Note that this does _not_ assume ASCII output. The process here is:
> 
> - transcribe the message to a Python 3 str (so Unicode code points)
> - replace embedded "From " to protect the mbox format
> - open the mbox for append - the _default_ encoding is utf-8
> - write the message in utf-8 because of the open mode
> 
> This sidesteps the library you're using which may well do something 
> ASCII based. And it has _never_ failed for me.
> 
Thanks Caneron, but I have now finally fixed my problem, see the new
thread.

-- 
Chris Green
·


More information about the Python-list mailing list