Dave Cinege wrote:
> Very often....make that very very very very very very very very very often,
> I find myself processing text in python that when .split()'ing a line, I'd
> like to exclude the split for a 'quoted' item...quoted because it contains
> whitespace or the sep char.
>
> For example:
>
> s = ' Chan: 11 SNR: 22 ESSID: "Spaced Out Wifi" Enc: On'
Even if you don't like Neal's more efficient regex-based version, the
necessary utility function to do a two-pass split operation really isn't that
tricky:
def split_quoted(text, sep=None, quote='"'):
sections = text.split(quote)
result = []
for idx, unquoted_text in enumerate(sections[::2]):
result.extend(unquoted_text.split(sep))
quoted = 2*idx+1
quoted_text = sections[quoted:quoted+1]
result.extend(quoted_text)
return result
>>> split_quoted(' Chan: 11 SNR: 22 ESSID: "Spaced Out Wifi" Enc: On')
['Chan:', '11', 'SNR:', '22', 'ESSID:', 'Spaced Out Wifi', 'Enc:', 'On']
Given that this function (or a regex based equivalent) is easy enough to add
if you do need it, I don't find the idea of increasing the complexity of the
basic split API particularly compelling.
Cheers,
Nick.
--
Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com