Extract value from a attribute in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • inFocus@sl.com

    Extract value from a attribute in a string


    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.
  • Gabriel Genellina

    #2
    Re: Extract value from a attribute in a string

    En Tue, 22 Jan 2008 23:45:22 -0200, <inFocus@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
    Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...


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

    --
    Gabriel Genellina

    Comment

    • inFocus@sl.com

      #3
      Re: Extract value from a attribute in a string

      On Wed, 23 Jan 2008 01:13:31 -0200, "Gabriel Genellina"
      <gagsl-py2@yahoo.com.a rwrote:
      >En Tue, 22 Jan 2008 23:45:22 -0200, <inFocus@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
      >
      >pyimport re
      >pytext = """ok name=foo
      >... in this line name=bar but
      >... here you get name = another thing
      >... is this what you want?"""
      >pyfor match in re.finditer(r"n ame\s*=\s*(\S+) ", text):
      >... print match.group(1)
      >...
      >foo
      >bar
      >another
      Thank you very much.

      Comment

      Working...