| |
- grabchunks(text, opener, closer)
- Get 'chunks' as marked by the specified opener and closer. Only the first
level of chunks will be returned. These may contain similar chunks, which
can be extracted by running the function again.
This is a generator: to get an indexable list of the chunks, use
list(grabchunks(...))
Example usage:
foo = "green <eggs <and>> multicoloured <spam>."
for chunk in grabchunks(foo, "<",">"):
print(chunk)
# eggs <and>
# spam
- removechunks(text, opener, closer)
- Strip out 'chunks', e.g. comments, as marked by the specified opener and
closer. E.g.
foo = "green <eggs <and>> multicoloured <spam>."
print removechunks(foo, "<", ">")
# green multicoloured .
- splitwithout(text, spliton, containermarks=[('"', '"')])
- Splits text in a similar manner to text.split(spliton), but ignoring
spliton where it occurs between start and end markers. E.g.
'a,<b,c>' could be split into ['a', '<b,c>'], rather than ['a', '<b', 'c>'].
You can specify any number of containers, and the text will only be split
where the delimiter occurs outside all of them. Each container should be a
tuple of the opening symbol and the closing symbol (even if they are the
same).
Example usage:
foo = 'Green, "Eggs, Spam", etc.'
splitwithout(foo, ',')
# ['Green', ' "Eggs, Spam"', ' etc.']
bar = "Do | {{Re | {{Mi | Fa }}}} | Sol | <La | Ti> | Do"
splitwithout(bar,'|', [('{{','}}'), ('<','>')])
# ['Do ', ' {{Re | {{Mi | Fa }}}} ', ' Sol ', ' <La | Ti> ', ' Do']
|