[Python-Dev] New string method - splitquoted

Dave Cinege dcinegemlists2-dated-1148418122.b868e0 at psychosis.com
Thu May 18 23:01:58 CEST 2006


On Thursday 18 May 2006 16:13, you wrote:
> Dave Cinege wrote:
> > For example:
> >
> > s = '      Chan: 11  SNR: 22  ESSID: "Spaced Out Wifi"  Enc: On'
>
> My complaint with this example is that you are just using the wrong tool
> to do this job. If I was going to do this, I would've immediately jumped
> on the regex-press train.
>
> wifi_info = re.match('^\s+'
>                       'Chan:\s+(?P<channel>[0-9]+)\s+'
>                       'SNR:\s+(?P<snr>[0-9]+)\s+'
>                       'ESSID:\s+"(?P<essid>[^"]*)"\s+'
>                       'Enc:\s+(?P<encryption>[a-zA-Z]+)'
>                       , s)

For the 5 years of been pythoning, I've used re probably twice. 
I find regex to be a tool of last resort, and quite a bit of effort to get 
right, as regex (for me) is quite prone it giving unintended results without 
a good deal of thought. I don't want to have to think. That's why I use 
python.  : )

.split() and slicing has always been python's holy grail for me, and I find it 
a lot easier to .replace() 'stray' chars with spaces or a delimiter and then 
split() that.  It's easier to read and (should be) a lot quicker to process 
then regex. (Which I care about, as I'm also often on embedded CPU's of a few 
hundred MHz)

So .split works just super duper.....but I keep running in to situations where 
I'd like a substr to be excluded from the split'ing.

The clearest one is excluding a 'quoted' string that has whitespace.
Here's another, be it, a very poor example: 

s = '\t\tFrequency:2.462 GHz (Channel 11)'	# This is real output from iwlist:
s.replace(':',')').replace(' (','))').split(None,-1,')')
['Frequency', '2.462 GHz', 'Channel 11']

I wanted to preserve the '2.462 GHz' substr. Let's assume, that could come out 
as '900 MHz' or '11.3409 GHz'. The above code gets what I want in 1 shot, 
either way. Show me an easier way, that doesn't need multiple splits, and 
string re-assembly, ....and I'll use it.

Dave



More information about the Python-Dev mailing list