How do I do this in Python 3 (string.join())?
Chris Green
cl at isbd.net
Thu Aug 27 04:16:21 EDT 2020
Cameron Simpson <cs at cskk.id.au> wrote:
> On 26Aug2020 15:09, Chris Green <cl at isbd.net> wrote:
> >2QdxY4RzWzUUiLuE at potatochowder.com wrote:
> >> Join bytes objects with a byte object:
> >>
> >> b"\n".join(popmsg[1])
> >
> >Aaahhh! Thank you (and the other reply).
>
> But note: joining bytes like strings is uncommon, and may indicate that
> you should be working in strings to start with. Eg you may want to
> convert popmsg from bytes to str and do a str.join anyway. It depends on
> exactly what you're dealing with: are you doing text work, or are you
> doing "binary data" work?
>
> I know many network protocols are "bytes-as-text, but that is
> accomplished by implying an encoding of the text, eg as ASCII, where
> characters all fit in single bytes/octets.
>
Yes, I realise that making everything a string before I start might be
the 'right' way to do things but one is a bit limited by what the mail
handling modules in Python provide.
E.g. in this case the only (well the only ready made) way to get a
POP3 message is using poplib and this just gives you a list of lines
made up of "bytes as text" :-
popmsg = pop3.retr(i+1)
I join the lines to feed them into mailbox.mbox() to create a mbox I
can analyse and also a message which can be sent using SMTP.
Should I be converting to string somewhere? I guess the POP3 and SMTP
libraries will cope with strings as input. Can I convert to string
after the join for example? If so, how? Can I just do:-
msgbytes = b'\n'.join(popmsg[1])
msgstr = str(mshbytes)
(Yes, I know it can be one line, I was just being explicit).
... or do I need to stringify the lines returned by popmsg() before
joining them together?
Thank you for all your help and comments!
(I'm a C programmer at heart, preceded by being an assembler
programmer. I started programming way back in the 1970s, I'm retired
now and Python is for relaxation (?) in my dotage)
--
Chris Green
ยท
More information about the Python-list
mailing list