How do I convert an iterator over bytes into a str?

markscottwright markscottwright at gmail.com
Wed Aug 19 18:49:07 CEST 2009


On Aug 18, 6:52 pm, "Jan Kaliszewski" <z... at chopin.edu.pl> wrote:
> 19-08-2009 o 00:24:20 markscottwright <markscottwri... at gmail.com> wrote:
>
> > What's the correct way to turn an iterator over bytes into a string?
> > This works, but, ewww:
> >     In [8]: "".join(iter("four score and seven years ago"))
> >     Out[8]: 'four score and seven years ago'
>
> But it is the correct way (and even recommended over s=s+t or s+=t, when
> applicable
> -- see:  http://docs.python.org/library/stdtypes.html#sequence-types-str-unico...).
>
> Cheers,
> *j
>
> --
> Jan Kaliszewski (zuo) <z... at chopin.edu.pl>

Thanks Jan (and all other responders).  I suppose I shouldn't be
surprised - it's a known wart (http://wiki.python.org/moin/
PythonWarts), but it just looks so darn wrong.  It is, as you point
out, much faster than "better looking" alternatives, though -
http://www.skymind.com/~ocrow/python_string/



More information about the Python-list mailing list