Extract value from a attribute in a string

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jan 22 22:13:31 EST 2008


En Tue, 22 Jan 2008 23:45:22 -0200, <inFocus at sl.com> escribió:

> I am looking for some help in reading a large text tile and extracting
> a value from an attribute? so I would need to find name=foo and
> extract just the value foo which can be at any location in the string.
> The attribute name will be in almost each line.

In this case a regular expression may be the right tool. See  
http://docs.python.org/lib/module-re.html

py> import re
py> text = """ok name=foo
... in this line name=bar but
... here you get name = another thing
... is this what you want?"""
py> for match in re.finditer(r"name\s*=\s*(\S+)", text):
...   print match.group(1)
...
foo
bar
another

-- 
Gabriel Genellina




More information about the Python-list mailing list