searching strings
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Mar 10 00:55:57 EDT 2009
En Mon, 09 Mar 2009 23:18:50 -0200, Daniel Dalton <d.dalton at iinet.net.au>
escribió:
> I'm writing a program where I search a variable (path), and see if it
> contains the whole string of variable name
> so:
> if name in path:
> ....
> else:
> ....
>
> One question about this, how can I make it do exactly what it's doing
> now, except ignore case? eg. if I do this:
> i="A"
> j="ab"
> i in j
> should return true... How do I do this?
Short answer:
if i.upper() in j.upper(): ...
Long answer: why upper()? why not lower()? Should use Unicode, casefold()
(inexistent), and canonical decomposition. See section 3.13 in the Unicode
specification.
Anyway, this may be "good enough":
from unicodedata import normalize
def caseinsensitivecmp(a,b):
return cmp(normalize("NFKD", a).upper(), normalize("NFKD", b).upper())
Apply the same transformation for the "in" operator that you're interested
in.
--
Gabriel Genellina
More information about the Python-list
mailing list