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

Chris Green cl at isbd.net
Fri Aug 28 03:56:26 EDT 2020


Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
> >But this is a really good job for a list comprehension:
> >sss = [str(word) for word in bbb]
> 
>   Are you all sure that "str" is really what you all want?
> 
Not absolutely, you no doubt have been following other threads related
to this one.  :-)


> |>>> b = b"b"
> |>>> str( b )
> |"b'b'"
> 
>   Maybe try to /decode/ the bytes?
> 
> |>>> b.decode( "ASCII" )
> |'b'
> 
> 
Therein lies the problem, the incoming byte stream *isn't* ASCII, it's
an E-Mail message which may, for example, have UTF-8 or other encoded
characters in it.  Hopefully it will have an encoding given in the
header but that's only if the sender is 'well behaved', one needs to
be able to handle almost anything and it must be done without 'manual'
interaction.

-- 
Chris Green
·


More information about the Python-list mailing list