need help extracting data from a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nephish@xit.net

    #1

    need help extracting data from a text file

    Hey there,
    i have a text file with a bunch of values scattered throughout it.
    i am needing to pull out a value that is in parenthesis right after a
    certain word,
    like the first time the word 'foo' is found, retrieve the values in the
    next set of parenthesis (bar) and it would return 'bar'

    i think i can use re to do this, but is there some easier way?
    thanks

  • Iain King

    #2
    Re: need help extracting data from a text file


    nephish@xit.net wrote:[color=blue]
    > Hey there,
    > i have a text file with a bunch of values scattered throughout it.
    > i am needing to pull out a value that is in parenthesis right after a
    > certain word,
    > like the first time the word 'foo' is found, retrieve the values in the
    > next set of parenthesis (bar) and it would return 'bar'
    >
    > i think i can use re to do this, but is there some easier way?
    > thanks[/color]

    well, you can use string.find with offsets, but an re is probably a
    cleaner way to go. I'm not sure which way is faster - it'll depend on
    how many times you're searching compared to the overhead of setting up
    an re.

    start = textfile.find(" foo(") + 4 # 4 being how long 'foo(' is
    end = textfile.find(" )", start)
    value = textfile[start:end]

    Iain

    Comment

    • nephish@xit.net

      #3
      Re: need help extracting data from a text file

      this is cool, it is only going to run about 10 times a day,

      the text is not written out like foo(bar) its more like
      foo blah blah blah (bar)

      the thing is , every few days the structure of the textfile may change,
      one of the reasons i wanted to avoid the re.

      thanks for the tip,

      Comment

      • Iain King

        #4
        Re: need help extracting data from a text file


        nephish@xit.net wrote:[color=blue]
        > this is cool, it is only going to run about 10 times a day,
        >
        > the text is not written out like foo(bar) its more like
        > foo blah blah blah (bar)
        >[/color]

        then I guess you worked this out, but just for completeness:

        keywordPos = textfile.find(" foo")
        start = textfile.find(" (", keywordPos)
        end = textfile.find(" )", start)
        value = textfile[start:end]


        Iain

        Comment

        • nephish@xit.net

          #5
          Re: need help extracting data from a text file

          um, wait. what you are doing here is easier than what i was doing after
          your first post.
          thanks a lot. this is going to work out ok.

          thanks again.
          sk

          Comment

          • Paul McGuire

            #6
            Re: need help extracting data from a text file

            <nephish@xit.ne t> wrote in message
            news:1131375863 .977379.120620@ f14g2000cwb.goo glegroups.com.. .[color=blue]
            > Hey there,
            > i have a text file with a bunch of values scattered throughout it.
            > i am needing to pull out a value that is in parenthesis right after a
            > certain word,
            > like the first time the word 'foo' is found, retrieve the values in the
            > next set of parenthesis (bar) and it would return 'bar'
            >
            > i think i can use re to do this, but is there some easier way?
            > thanks
            >[/color]
            Using string methods to locate the 'foo' instances is by far the fastest way
            to go.

            If your requirements get more complicated, look into using pyparsing
            (http://pyparsing.sourceforge.net). Here is a pyparsing rendition of this
            problem. This does three scans through some sample data - the first lists
            all matches, the second ignores matches if they are found inside a quoted
            string, and the third reports only the third match. This kind of
            context-sensitive matching gets trickier with basic string and re tools.

            -- Paul

            data = """
            i have a text file with a bunch of foo(bar1) values scattered throughout it.
            i am needing to pull out a value that foo(bar2) is in parenthesis right
            after a
            certain word,
            like the foo(bar3) first time the word 'foo' is found, retrieve the values
            in the
            next set of parenthesis foo(bar4) and it would return 'bar'
            do we want to skip things in quotes, such as 'foo(barInQuote s)'?
            """

            from pyparsing import Literal,SkipTo, quotedString

            pattern = Literal("foo") + "(" + SkipTo(")").set ResultsName("pa yload") + ")"

            # report all occurrences of xxx found in "foo(xxx)"
            for tokens,start,en d in pattern.scanStr ing(data):
            print tokens.payload, "at location", start
            print

            # ignore quoted strings
            pattern.ignore( quotedString)
            for tokens,start,en d in pattern.scanStr ing(data):
            print tokens.payload, "at location", start
            print

            # only report 3rd occurrence
            tokenMatch = {'foo':0}
            def thirdTimeOnly(s trg,loc,tokens) :
            word = tokens[0]
            if word in tokenMatch:
            tokenMatch[word] += 1
            if tokenMatch[word] != 3:
            raise ParseException( strg,loc,"wrong occurrence of token")

            pattern.setPars eAction(thirdTi meOnly)
            for tokens,start,en d in pattern.scanStr ing(data):
            print tokens.payload, "at location", start
            print

            Prints:
            bar1 at location 36
            bar2 at location 116
            bar3 at location 181
            bar4 at location 278
            barInQuotes at location 360

            bar1 at location 36
            bar2 at location 116
            bar3 at location 181
            bar4 at location 278

            bar3 at location 181


            Comment

            • Kent Johnson

              #7
              Re: need help extracting data from a text file

              nephish@xit.net wrote:[color=blue]
              > Hey there,
              > i have a text file with a bunch of values scattered throughout it.
              > i am needing to pull out a value that is in parenthesis right after a
              > certain word,
              > like the first time the word 'foo' is found, retrieve the values in the
              > next set of parenthesis (bar) and it would return 'bar'
              >
              > i think i can use re to do this, but is there some easier way?[/color]

              It's pretty easy with an re:
              [color=blue][color=green][color=darkred]
              >>> import re
              >>> fooRe = re.compile(r'fo o.*?\((.*?)\)')
              >>> fooRe.search('f oo(bar)').group (1)[/color][/color][/color]
              'bar'[color=blue][color=green][color=darkred]
              >>> fooRe.search('T his is a foo bar baz blah blah (bar)').group(1 )[/color][/color][/color]
              'bar'

              Kent

              Comment

              • Tom Anderson

                #8
                Re: need help extracting data from a text file

                On Mon, 7 Nov 2005, Kent Johnson wrote:
                [color=blue]
                > nephish@xit.net wrote:
                >[color=green]
                >> i have a text file with a bunch of values scattered throughout it. i am
                >> needing to pull out a value that is in parenthesis right after a
                >> certain word, like the first time the word 'foo' is found, retrieve the
                >> values in the next set of parenthesis (bar) and it would return 'bar'[/color]
                >
                > It's pretty easy with an re:
                >[color=green][color=darkred]
                >>>> import re
                >>>> fooRe = re.compile(r'fo o.*?\((.*?)\)')[/color][/color][/color]

                Just out of interest, i've never really got into using non-greedy
                quantifiers (i use them from time to time, but hardly ever feel the need
                for them), so my instinct would have been to write this as:
                [color=blue][color=green][color=darkred]
                >>> fooRe = re.compile(r"fo o[^(]*\(([^)]*)\)")[/color][/color][/color]

                Is there any reason to use one over the other?
                [color=blue][color=green][color=darkred]
                >>>> fooRe.search('f oo(bar)').group (1)[/color][/color]
                > 'bar'[color=green][color=darkred]
                >>>> fooRe.search('T his is a foo bar baz blah blah (bar)').group(1 )[/color][/color]
                > 'bar'[/color]

                Ditto.

                tom

                --
                [of Muholland Drive] Cancer is pretty ingenious too, but its best to
                avoid. -- Tex

                Comment

                Working...