A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2006-May/065047.html below:

[Python-Dev] New string method - splitquoted

[Python-Dev] New string method - splitquotedNick Coghlan ncoghlan at gmail.com
Thu May 18 15:20:52 CEST 2006
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   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4