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

M

markscottwright

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

Mark Lawrence

markscottwright said:
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 'str'>
 
J

John Machin

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).
 
C

Carl Banks

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
 
M

markscottwright

19-08-2009 o 00:24:20 markscottwright said:
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

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/
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top