Python 3 how to convert a list of bytes objects to a list of strings?
Cameron Simpson
cs at cskk.id.au
Sat Aug 29 17:59:40 EDT 2020
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.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Python-list
mailing list