[Tutor] Re: Testing for punctuation in a string
Greg Brunet
gbrunet at sempersoft.com
Mon Oct 13 04:30:37 EDT 2003
Hi Anna:
Thanks for your response.
"Anna Ravenscroft" <anna at aleax.it> wrote in message
news:200310122335.34640.anna at aleax.it...
> On Sunday 12 October 2003 06:52 pm, Alan Gauld wrote:
> > > If s is my list of fieldnames (s='fld1,fld2,fld-bad'), I'm not
sure
>
> Make sure your list of fieldnames has quotes around each item, not
just the
> beginning and end of the list...
Actually, I the string is getting loaded by a call to getopt, so I
wanted to show what I originally received in case there wasn't a need to
split it into the actual names. If I want to look at it by names (which
I eventually did), my first step was to use:
fields = s.split(',')
> > what to do next. I would probably split the string & test it a
character
> > at a time, but I am hoping for something better. Thanks,
>
> Ouch. Sounds long and unpleasant.
Yeah, but it was a quick and easy first approach. For various reasons,
I ended up using regex, thought that might be a bit heavyweight for what
I needed:
p=re.compile('^[a-zA-Z]\w*$')
def fldNameValid(fldName):
return p.match(fldName) != None
> I like the nifty new sets module (available in 2.3). You can use a
list
> comprehension (as Alan suggested) but if you have more than one type
of "bad"
> punctuation in a particular fieldname, you'll get duplicates. If you
use the
> new sets module, you can eliminate the duplicates.
>
> Hope this gives you some ideas... Have fun.
>
> Anna
I will look into sets though, so I am familiar with them for the next
time. Thanks again,
--
Greg
More information about the Tutor
mailing list