Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a string into an iterator

Does python have a build-in (meaning in the standard libraries) to do a split on strings that produces an iterator rather than a list? I have in mind working on very long strings and not needing to consume most of the string.

like image 559
pythonic metaphor Avatar asked Jan 03 '11 16:01

pythonic metaphor


People also ask

How do you split a string in python with iteration?

Not directly splitting strings as such, but the re module has re. finditer() (and corresponding finditer() method on any compiled regular expression). An example of how to use re. finditer() to iterate split strings would be helpful.

What does Splitlines mean in Python?

The splitlines() method splits a string into a list. The splitting is done at line breaks.


1 Answers

Not directly splitting strings as such, but the re module has re.finditer() (and corresponding finditer() method on any compiled regular expression).

@Zero asked for an example:

>>> import re >>> s = "The quick    brown\nfox" >>> for m in re.finditer('\S+', s): ...     print(m.span(), m.group(0)) ...  (0, 3) The (4, 9) quick (13, 18) brown (19, 22) fox 
like image 110
Duncan Avatar answered Sep 19 '22 06:09

Duncan