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

Discussion in 'Python' started by markscottwright, Aug 18, 2009.

  1. 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'
    markscottwright, Aug 18, 2009
    #1
    1. Advertising

  2. 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.
    Mark Lawrence, Aug 18, 2009
    #2
    1. Advertising

  3. Jan Kaliszewski, Aug 19, 2009
    #3
  4. markscottwright

    John Machin Guest

    On Aug 19, 8:24 am, 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'


    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).
    John Machin, Aug 19, 2009
    #4
  5. markscottwright

    Carl Banks Guest

    On Aug 18, 3:24 pm, 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>'


    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
    Carl Banks, Aug 19, 2009
    #5
  6. On Aug 18, 6:52 pm, "Jan Kaliszewski" <> wrote:
    > 19-08-2009 o 00:24:20 markscottwright <> 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) <>


    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/
    markscottwright, Aug 19, 2009
    #6
  7. On Wed, 19 Aug 2009 20:04:12 -0700, alex23 wrote:

    > markscottwright <> 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
    Steven D'Aprano, Aug 20, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    470
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    614
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    327
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,648
    Oliver Wong
    Aug 3, 2006
  5. Replies:
    6
    Views:
    629
    Jim Langston
    Oct 30, 2005
Loading...

Share This Page