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/stdtypes.html#sequence-types-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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I convert an iterator over bytes into a str?
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)) -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I convert an iterator over bytes into a str?
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/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange). Cheers, *j -- Jan Kaliszewski (zuo) z...@chopin.edu.pl -- http://mail.python.org/mailman/listinfo/python-list
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). -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list