How do I do this in Python 3 (string.join())?
D'Arcy Cain
darcy at VybeNetworks.com
Wed Aug 26 09:53:36 EDT 2020
On 2020-08-26 09:22, Chris Green wrote:
> I have the following line in Python 2:-
>
> msgstr = string.join(popmsg[1], "\n") # popmsg[1] is a list containing the lines of the message
>
> ... so I changed it to:-
>
> s = "\n"
> msgstr = s.join(popmsg[1]) # popmsg[1] is a list containing the lines of the message
>
> However this still doesn't work because popmsg[1] isn't a list of
> strings, I get the error:-
>
> TypeError: sequence item 0: expected str instance, bytes found
>
> So how do I do this? I can see clumsy ways by a loop working through
> the list in popmsg[1] but surely there must be a way that's as neat
> and elegant as the Python 2 way was?
Well, the simple fix is to set s to b"\n" but that may not solve all of your
problems. The issue is that popmsg[1] is a list of bytes. You probably
want a list of strings. I would look further back and think about getting a
list of strings in the first place. Without knowing how popmsg was created
we can't tell you how to do that.
Of course, if a bytes object is what you want then the above will work. You
can also convert to string after the join.
Cheers.
--
D'Arcy J.M. Cain
Vybe Networks Inc.
A unit of Excelsior Solutions Corporation - Propelling Business Forward
http://www.VybeNetworks.com/
IM:darcy at VybeNetworks.com VoIP: sip:darcy at VybeNetworks.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20200826/c8c5c92e/attachment.sig>
More information about the Python-list
mailing list