def insertBefore(list, newElement, referenceElement):
for i in range(0, len(list)):
if list[i] == referenceElement
list.insert(i, newElement)
break
Tuesday, June 17, 2008
Python List insertBefore
Thursday, June 12, 2008
Using Regular Expressions To Get A Substring In Python
Using the ElementTree to parse XML, I am getting tags that look like this: "{namespace}tag". I need an easy way to strip off the namespace.
>>> import re
>>> str = "{blat}tag"
>>> re.findall(r'{.*}', str)
['{blat}']
>>> str += "{goo}asdf{boing}blat"
>>> re.findall(r'{.*}', str)
['{blat}tag{goo}asdf{boing}']
>>> re.findall(r'{.*?}', str)
['{blat}', '{goo}', '{boing}']
>>> re.findall(r'{.*?}', str)[0]
'{blat}'
>>> goo = re.findall(r'{.*?}', str)[0]
>>> goo
'{blat}'
>>> goo[1:len(goo)-1]
'blat'
I'm sure the { and } could be stripped off with the regular expression. I still have to play with it.
>>> import re
>>> str = "{blat}tag"
>>> re.findall(r'{.*}', str)
['{blat}']
>>> str += "{goo}asdf{boing}blat"
>>> re.findall(r'{.*}', str)
['{blat}tag{goo}asdf{boing}']
>>> re.findall(r'{.*?}', str)
['{blat}', '{goo}', '{boing}']
>>> re.findall(r'{.*?}', str)[0]
'{blat}'
>>> goo = re.findall(r'{.*?}', str)[0]
>>> goo
'{blat}'
>>> goo[1:len(goo)-1]
'blat'
I'm sure the { and } could be stripped off with the regular expression. I still have to play with it.
Subscribe to:
Posts (Atom)
