Extract value from a attribute in a string

I

inFocus

Hello,

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.

Thank you for any suggestions.
 
G

Gabriel Genellina

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
 
I

inFocus

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

Thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top