[Python-Dev] New string method - splitquoted

Dave Cinege dcinege-mlists-dated-1148357217.fc23eb at psychosis.com
Thu May 18 06:06:51 CEST 2006


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'

If I want to yank the essid in the above example, it's a pain. But with my new 
dandy split quoted method, we have a 3rd argument to .split() that we can 
spec the quote delimiter where no splitting will occur, and the quote char 
will be dropped:

	s.split(None,-1,'"')[5]
	'Spaced Out Wifi'

Attached is a proof of concept patch against 
Python-2.4.1/Objects/stringobject.c  that implements this. It is limited to 
whitespace splitting only. (sep == None)

As implemented the quote delimiter also doubles as an additional separator for 
the spliting out a substr. 

For example:
	'There is"no whitespace before these"quotes'.split(None,-1,'"')
	['There', 'is', 'no whitespace before these', 'quotes']

This is useful, but possibly better put into practice as a separate method??

Comments please.

Dave
-------------- next part --------------
A non-text attachment was scrubbed...
Name: splitquoted-20060516_stringobject.c.diff
Type: text/x-diff
Size: 3169 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20060518/ae6f5a63/attachment.bin 


More information about the Python-Dev mailing list