Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How do I convert an iterator over bytes into a str? (http://www.velocityreviews.com/forums/t695255-how-do-i-convert-an-iterator-over-bytes-into-a-str.html)

markscottwright 08-18-2009 10:24 PM

How do I convert an iterator over bytes into a str?
 
This does what I expected:
In [6]: list(iter([1,2,3,4,5]))
Out[6]: [1, 2, 3, 4, 5]

But this appears to be doing a __repr__ rather than making me a nice
string:
In [7]: str(iter("four score and seven years ago"))
Out[7]: '<iterator object at 0x0139F190>'

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'

Mark Lawrence 08-18-2009 10:54 PM

Re: How do I convert an iterator over bytes into a str?
 
markscottwright wrote:
> This does what I expected:
> In [6]: list(iter([1,2,3,4,5]))
> Out[6]: [1, 2, 3, 4, 5]
>
> But this appears to be doing a __repr__ rather than making me a nice
> string:
> In [7]: str(iter("four score and seven years ago"))
> Out[7]: '<iterator object at 0x0139F190>'
>
> 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'

You've started with a string.
>>> type("four score and seven years ago")

<type 'str'>

--
Kindest regards.

Mark Lawrence.


Jan Kaliszewski 08-18-2009 11:52 PM

Re: How do I convert an iterator over bytes into a str?
 
19-08-2009 o 00:24:20 markscottwright <markscottwright@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/stdty...buffer-xrange).

Cheers,
*j

--
Jan Kaliszewski (zuo) <zuo@chopin.edu.pl>

John Machin 08-19-2009 02:32 AM

Re: How do I convert an iterator over bytes into a str?
 
On Aug 19, 8:24*am, markscottwright <markscottwri...@gmail.com> wrote:
> This does what I expected:
> * * In [6]: list(iter([1,2,3,4,5]))
> * * Out[6]: [1, 2, 3, 4, 5]
>
> But this appears to be doing a __repr__ rather than making me a nice
> string:
> * *In [7]: str(iter("four score and seven years ago"))
> * *Out[7]: '<iterator object at 0x0139F190>'
>
> 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'


There is no such thing as an "iterator over bytes" in Python 2.x.
There is no such concept as "convert an iterator over <anything> into
a str" object.

What you have is an iterator over str objects of length 1. To do what
you appear to actually want to do (concatenate a bunch of strings), it
is recomemnded to use ''.join(str_iterable).

Carl Banks 08-19-2009 03:02 AM

Re: How do I convert an iterator over bytes into a str?
 
On Aug 18, 3:24*pm, markscottwright <markscottwri...@gmail.com> wrote:
> This does what I expected:
> * * In [6]: list(iter([1,2,3,4,5]))
> * * Out[6]: [1, 2, 3, 4, 5]
>
> But this appears to be doing a __repr__ rather than making me a nice
> string:
> * *In [7]: str(iter("four score and seven years ago"))
> * *Out[7]: '<iterator object at 0x0139F190>'


Unfortunately, str() is overloaded in that it tries to be both a sorta-
pretty-printer and a constructor. You're trying to use it as a
constructor, but it wants to be a sorta-pretty-printer here.

Anyway, str is different from other container objects since, unlike
other containers, strings can't contain arbitrary Python objects.


> 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'


This is the correct way.

If the syntax bothers you can always do this:

str.join("",iter("four score"))

I think "".join is ugly as hell but in this case convenience beats
beauty for me.


Carl Banks

markscottwright 08-19-2009 04:49 PM

Re: How do I convert an iterator over bytes into a str?
 
On Aug 18, 6:52*pm, "Jan Kaliszewski" <z...@chopin.edu.pl> wrote:
> 19-08-2009 o 00:24:20 markscottwright <markscottwri...@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/stdty...-str-unico...).
>
> Cheers,
> *j
>
> --
> Jan Kaliszewski (zuo) <z...@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/

Steven D'Aprano 08-20-2009 04:20 AM

Re: How do I convert an iterator over bytes into a str?
 
On Wed, 19 Aug 2009 20:04:12 -0700, alex23 wrote:

> markscottwright <markscottwri...@gmail.com> wrote:
>> 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.

>
> Don't forget that it's exceptionally easy to create your own mechanism
> for doing this:
>
> def join(seq, sep):
> return sep.join(map(str, seq))


Oh oh oh my brain hurts!!! Neither "seq" nor "sep" are real words, both
are abbreviations, they differ by a single letter, and the two letters
are mirror images of each other!!!

This is a recipe for confusion when people get the order of sep and seq
mixed up. Hopefully in real life code, you'd use a less easily confused
function signature -- even just spelling out sequence and separator in
full would reduce confusion to essentially zero.



--
Steven


All times are GMT. The time now is 02:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.