This sounds quite an easy thing to do but I can't find how to do it
elegantly.
I have a list of bytes class objects (i.e. a list containing sequences
of bytes, which are basically text) and I want to convert it to a list
of string objects.
One of the difficulties of finding out how to do this is
On Fri, Aug 28, 2020 at 6:36 AM Chris Green wrote:
>
> This sounds quite an easy thing to do but I can't find how to do it
> elegantly.
>
> I have a list of bytes class objects (i.e. a list containing sequences
> of bytes, which are basically text) and I want to convert it to a list
> of string ob
Are you sure you want `str()`?
>>> str(b'aaa')
"b'aaa'"
Probably you want:
map(lambda x: x.decode(), bbb)
--
https://mail.python.org/mailman/listinfo/python-list
On 27Aug2020 23:54, Marco Sulla wrote:
>Are you sure you want `str()`?
>
str(b'aaa')
>"b'aaa'"
>
>Probably you want:
>
>map(lambda x: x.decode(), bbb)
_And_ you need to know the encoding of the text in the bytes. The above
_assumes_ UTF-8 because that is the default for bytes.decode, and if
Stefan Ram wrote:
> Chris Angelico 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. :-)
Chris Angelico wrote:
> On Fri, Aug 28, 2020 at 6:36 AM Chris Green wrote:
> >
> > This sounds quite an easy thing to do but I can't find how to do it
> > elegantly.
> >
> > I have a list of bytes class objects (i.e. a list containing sequences
> > of bytes, which are basically text) and I want t
Cameron Simpson wrote:
> On 27Aug2020 23:54, Marco Sulla wrote:
> >Are you sure you want `str()`?
> >
> str(b'aaa')
> >"b'aaa'"
> >
> >Probably you want:
> >
> >map(lambda x: x.decode(), bbb)
>
> _And_ you need to know the encoding of the text in the bytes. The above
> _assumes_ UTF-8 beca
On 28Aug2020 08:56, Chris Green wrote:
>Stefan Ram wrote:
>> Chris Angelico 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 follow
Cameron Simpson wrote:
> On 28Aug2020 08:56, Chris Green wrote:
> >Stefan Ram wrote:
> >> Chris Angelico 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
Richard Damon wrote:
> On 8/28/20 7:50 AM, Karsten Hilbert wrote:
> >>> No interpreation requires, since parsing failed. Then you can start
> >>> dealing with these exceptions. _Do not_ write unparsable messages into
> >>> an mbox!
> >>>
> >> Maybe I shouldn't but Python 2 has been managing to do
Stefan Ram wrote:
> Chris Green writes:
> >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
On Fri, Aug 28, 2020 at 10:51 PM Chris Green wrote:
>
> > One possible solution in Python3 is to decode the byte string using an
> > encoding that allows all 256 byte values, so it won't raise any encoding
> > errors, just give your possibly non-sense characters for non-ASCII text.
> >
> But this
On 8/28/20 8:39 AM, Chris Green wrote:
> Richard Damon wrote:
>> On 8/28/20 7:50 AM, Karsten Hilbert wrote:
> No interpreation requires, since parsing failed. Then you can start
> dealing with these exceptions. _Do not_ write unparsable messages into
> an mbox!
>
Maybe I shoul
Chris Angelico wrote:
>
> Also, if you're parsing an email message, you can and should be doing
> so with respect to the encoding(s) stipulated in the headers, after
> which you will have valid Unicode text.
>
But not all E-Mail messages are 'well behaved', the above works fine
if the headers sp
On Fri, Aug 28, 2020 at 11:24 PM Chris Green wrote:
>
> Chris Angelico wrote:
> >
> > Also, if you're parsing an email message, you can and should be doing
> > so with respect to the encoding(s) stipulated in the headers, after
> > which you will have valid Unicode text.
> >
> But not all E-Mail
On 8/28/20 8:44 AM, Chris Green wrote:
> Stefan Ram wrote:
>> Chris Green writes:
>>> 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
On 28Aug2020 12:26, Chris Green wrote:
>Cameron Simpson wrote:
>> POP3 is presumably handing you bytes containing a message. If the
>> Python
>> email.BytesParser doesn't handle it, stash the raw bytes _elsewhere_ in
>> a distinct file in some directory.
>>
>> with open('evil_msg_bytes', 'wb
On 2020-08-27, Marco Sulla wrote:
> Are you sure you want `str()`?
>
str(b'aaa')
> "b'aaa'"
>
> Probably you want:
>
> map(lambda x: x.decode(), bbb)
If you're an old Scheme or Lisp programmer. :)
This is probably the more usual way to spell it:
sss = [x.decode() for x in bbb]
--
ht
On 2020-08-27, Chris Green wrote:
> bbb = [b'aaa', b'bbb', b'ccc']
> sss = []
> for i in range(0, 2):
> sss.append(str(bbb[i])
>
> but that does seem a bit clumsy. Is there a better way?
sss = [str(s) for s in bbb]
--
Grant
--
https://mail.python.org/mailman/listinfo/pyth
On 2020-08-28, Chris Green wrote:
> Maybe I shouldn't but Python 2 has been managing to do so for several
> years without any issues. I know I *could* put the exceptions in a
> bucket somewhere and deal with them separately but I'd really rather
> not.
Then just leave it as bytes and do whateve
Dennis Lee Bieber wrote:
> On Fri, 28 Aug 2020 12:26:07 +0100, Chris Green declaimed the
> following:
>
>
>
> >Maybe I shouldn't but Python 2 has been managing to do so for several
> >years without any issues. I know I *could* put the exceptions in a
> >bucket somewhere and deal with them sep
Chris Green wrote:
> Dennis Lee Bieber wrote:
> > On Fri, 28 Aug 2020 12:26:07 +0100, Chris Green declaimed
> > the
> > following:
> >
> >
> >
> > >Maybe I shouldn't but Python 2 has been managing to do so for several
> > >years without any issues. I know I *could* put the exceptions in a
>
On 8/29/20 11:50 AM, Chris Green wrote:
> Chris Green wrote:
>> Dennis Lee Bieber wrote:
>>> On Fri, 28 Aug 2020 12:26:07 +0100, Chris Green declaimed
>>> the
>>> following:
>>>
>>>
>>>
Maybe I shouldn't but Python 2 has been managing to do so for several
years without any issues. I
On 29Aug2020 16:50, Chris Green 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
Karsten Hilbert wrote:
> > However the problem appears to be that internally in Python 3 mailbox
> > class there is an assumption that it's being given 'ascii'.
>
> Do you really _need_ the mailbox class ? From what you've
> written so far my understanding was that you receive data
> (bytes) and
Cameron Simpson wrote:
> On 29Aug2020 16:50, Chris Green 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 (
> >Are you sure you want `str()`?
> >
> str(b'aaa')
> >"b'aaa'"
> >
> >Probably you want:
> >
> >map(lambda x: x.decode(), bbb)
>
> _And_ you need to know the encoding of the text in the bytes. The above
> _assumes_ UTF-8 because that is the default for bytes.decode, and if
> that is _not_ wha
> > No interpreation requires, since parsing failed. Then you can start
> > dealing with these exceptions. _Do not_ write unparsable messages into
> > an mbox!
> >
> Maybe I shouldn't but Python 2 has been managing to do so for several
> years without any issues.
I am inclined to congratulate you
> I want to transport the message into my mbox and Python 3 won't do it
> without knowing how it's encoded whereas Python 2 just stuffed it in
> there 'as is'.
>
> I want Python 3's mailbox class to juyst put what I tell it (even if
> mis-formatted or mis-encoded) into the mbox.
I guess using the
> However the problem appears to be that internally in Python 3 mailbox
> class there is an assumption that it's being given 'ascii'.
Do you really _need_ the mailbox class ? From what you've
written so far my understanding was that you receive data
(bytes) and want to append that to a file (which
On 8/28/20 7:50 AM, Karsten Hilbert wrote:
>>> No interpreation requires, since parsing failed. Then you can start
>>> dealing with these exceptions. _Do not_ write unparsable messages into
>>> an mbox!
>>>
>> Maybe I shouldn't but Python 2 has been managing to do so for several
>> years without an
On 8/28/20 7:50 AM, Karsten Hilbert wrote:
>>> No interpreation requires, since parsing failed. Then you can start
>>> dealing with these exceptions. _Do not_ write unparsable messages into
>>> an mbox!
>>>
>> Maybe I shouldn't but Python 2 has been managing to do so for several
>> years without an
On Fri, Aug 28, 2020 at 10:32 PM Richard Damon wrote:
>
> This might be one of the cases where Python 2's lack handling of string
> vs bytes was an advantage.
>
> If he was just scanning the message for specific ASCII strings, then not
> getting the full message decoded write is unlikely to have b
On 2020-08-28 08:30, Richard Damon wrote:
> This might be one of the cases where Python 2's lack handling of string
> vs bytes was an advantage.
For English speaking Americans.
> Python2 handled that sort of case quite easily. Python 3 on the other
> hand, will have issue converting the byte mess
On 8/29/20 3:31 PM, Karsten Hilbert wrote:
>> However the problem appears to be that internally in Python 3 mailbox
>> class there is an assumption that it's being given 'ascii'.
> Do you really _need_ the mailbox class ? From what you've
> written so far my understanding was that you receive data
> Just appending a message as a raw file to a mailbox, doesn't properly
> add it as a new message. You need to add a From: line to the front, and
> then go through the message and alter any line that begins as "From:"
> (and possibly any line that begins with something like ">From:" or
> ">>From:"
36 matches
Mail list logo