Iterate over the lines of a string
Posted
by Space_C0wb0y
on Stack Overflow
See other posts from Stack Overflow
or by Space_C0wb0y
Published on 2010-06-16T15:13:55Z
Indexed on
2010/06/16
15:22 UTC
Read the original article
Hit count: 147
I have a multi-line string defined like this:
foo = """
this is
a multi-line string.
"""
I need an iterator that iterates over the individual lines of that string. I could of course do it like this:
lineiterator = iter(foo.splitlines())
Is there a more direct way of doing this? In this scenario the string has to traversed once for the splitting, and then again by the parser. It doesn't matter in my test-case, since the string is very short there, I am just asking out of curiosity. Python has so many useful and efficient built-ins for such stuff, but I could find nothing that suits this need.
© Stack Overflow or respective owner