Looping issues

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

    #1

    Looping issues

    What I am trying to do is compare two files to each other.

    If the 2nd file contains the same line the first file contains, I want
    to print it. I wrote up the following code:



    correct_setting s = open("C:\Python 25\Scripts\Outp ut
    \correct_settin gs.txt","r")
    current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")

    for line in correct_setting s:
    for val in current_setting s:
    if val == line:
    print line + " found."


    correct_setting s.close()
    current_setting s.close()


    For some reason this only looks at the first line of the
    correct_setting s.txt file. Any ideas as to how i can loop through each
    line of the correct_setting s file instead of just looking at the first?

  • anglozaxxon

    #2
    Re: Looping issues

    On Apr 5, 2:01 pm, brochu...@gmail .com wrote:
    What I am trying to do is compare two files to each other.
    >
    If the 2nd file contains the same line the first file contains, I want
    to print it. I wrote up the following code:
    >
    correct_setting s = open("C:\Python 25\Scripts\Outp ut
    \correct_settin gs.txt","r")
    current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
    >
    for line in correct_setting s:
    for val in current_setting s:
    if val == line:
    print line + " found."
    >
    correct_setting s.close()
    current_setting s.close()
    >
    For some reason this only looks at the first line of the
    correct_setting s.txt file. Any ideas as to how i can loop through each
    line of the correct_setting s file instead of just looking at the first?
    Instead of "for line in correct_setting s", try "for line in
    correct_setting s.readlines()".

    Comment

    • brochu121@gmail.com

      #3
      Re: Looping issues

      On Apr 5, 2:18 pm, "anglozaxxo n" <anglozax...@gm ail.comwrote:
      On Apr 5, 2:01 pm, brochu...@gmail .com wrote:
      >
      >
      >
      What I am trying to do is compare two files to each other.
      >
      If the 2nd file contains the same line the first file contains, I want
      to print it. I wrote up the following code:
      >
      correct_setting s = open("C:\Python 25\Scripts\Outp ut
      \correct_settin gs.txt","r")
      current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
      >
      for line in correct_setting s:
      for val in current_setting s:
      if val == line:
      print line + " found."
      >
      correct_setting s.close()
      current_setting s.close()
      >
      For some reason this only looks at the first line of the
      correct_setting s.txt file. Any ideas as to how i can loop through each
      line of the correct_setting s file instead of just looking at the first?
      >
      Instead of "for line in correct_setting s", try "for line in
      correct_setting s.readlines()".
      That Still didnt fix it. Same output

      Comment

      • Larry Bates

        #4
        Re: Looping issues

        brochu121@gmail .com wrote:
        What I am trying to do is compare two files to each other.
        >
        If the 2nd file contains the same line the first file contains, I want
        to print it. I wrote up the following code:
        >
        >
        >
        correct_setting s = open("C:\Python 25\Scripts\Outp ut
        \correct_settin gs.txt","r")
        current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
        >
        for line in correct_setting s:
        for val in current_setting s:
        if val == line:
        print line + " found."
        >
        >
        correct_setting s.close()
        current_setting s.close()
        >
        >
        For some reason this only looks at the first line of the
        correct_setting s.txt file. Any ideas as to how i can loop through each
        line of the correct_setting s file instead of just looking at the first?
        >
        If the files aren't terribly large (not tested):

        correct_lines=o pen(r"C:\Python 25\Scripts\Outp ut" \
        "\correct_setti ngs.txt", "r").readlines( )

        current_lines=o pen(r"C:\Python 25\Scripts\Outp ut\output.txt",
        "r").readlines( )

        for line in current_setting s:
        if line in correct_lines:
        print line + " found"

        This does what you asked for but somehow I don't think it is
        what you want. I would suggest that you take a look at difflib.

        Somththing along the lines of:

        import difflib

        correct_lines=o pen(r"C:\Python 25\Scripts\Outp ut" \
        "\correct_setti ngs.txt", "r").readlines( )

        current_lines=o pen(r"C:\Python 25\Scripts\Outp ut\output.txt",
        "r").readlines( )

        delta=difflib.u nified_diff(cor rect_lines, current_lines)
        diffs=''.join(d elta)

        print diffs

        Will show you the lines that are different and some lines
        around it for context.

        -Larry

        Comment

        • Bruno Desthuilliers

          #5
          Re: Looping issues

          anglozaxxon a écrit :
          On Apr 5, 2:01 pm, brochu...@gmail .com wrote:
          >
          (snip)
          >>For some reason this only looks at the first line of the
          >>correct_setti ngs.txt file. Any ideas as to how i can loop through each
          >>line of the correct_setting s file instead of just looking at the first?
          >
          >
          Instead of "for line in correct_setting s", try "for line in
          correct_setting s.readlines()".
          What is this supposed to change ? (apart from loading the whole file in
          memory, which may not be a problem here - config files are usually not
          that big - but might become one for huge files...)

          Comment

          • anglozaxxon

            #6
            Re: Looping issues

            On Apr 5, 2:27 pm, Larry Bates <larry.ba...@we bsafe.comwrote:
            brochu...@gmail .com wrote:
            What I am trying to do is compare two files to each other.
            >
            If the 2nd file contains the same line the first file contains, I want
            to print it. I wrote up the following code:
            >
            correct_setting s = open("C:\Python 25\Scripts\Outp ut
            \correct_settin gs.txt","r")
            current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
            >
            for line in correct_setting s:
            for val in current_setting s:
            if val == line:
            print line + " found."
            >
            correct_setting s.close()
            current_setting s.close()
            >
            For some reason this only looks at the first line of the
            correct_setting s.txt file. Any ideas as to how i can loop through each
            line of the correct_setting s file instead of just looking at the first?
            >
            If the files aren't terribly large (not tested):
            >
            correct_lines=o pen(r"C:\Python 25\Scripts\Outp ut" \
            "\correct_setti ngs.txt", "r").readlines( )
            >
            current_lines=o pen(r"C:\Python 25\Scripts\Outp ut\output.txt",
            "r").readlines( )
            >
            for line in current_setting s:
            if line in correct_lines:
            print line + " found"
            >
            This does what you asked for but somehow I don't think it is
            what you want. I would suggest that you take a look at difflib.
            >
            Somththing along the lines of:
            >
            import difflib
            >
            correct_lines=o pen(r"C:\Python 25\Scripts\Outp ut" \
            "\correct_setti ngs.txt", "r").readlines( )
            >
            current_lines=o pen(r"C:\Python 25\Scripts\Outp ut\output.txt",
            "r").readlines( )
            >
            delta=difflib.u nified_diff(cor rect_lines, current_lines)
            diffs=''.join(d elta)
            >
            print diffs
            >
            Will show you the lines that are different and some lines
            around it for context.
            >
            -Larry
            Sorry my solution didn't work. The only other thing I can think of is
            that something is screwy with the newlines, although this seems
            silly. I've heard Python has "universal newline" support, meaning \n
            = \r = \r\n, etc.

            Try printing the contents of the file in its entirety:
            print current_setting s.read()
            And see if it prints the entire file, or just the first line. I can't
            imagine it won't print the whole thing. Next, do print
            current_setting s.read().replac e('\\','\\\\'). This will make the
            escape characters visible, so you can see exactly what type of
            newlines it's printing. If the file is in Unix format and you're on
            Windows, Python may be assuming the latter and not breaking lines
            correctly. Post what it prints.

            Nick

            Comment

            • Bruno Desthuilliers

              #7
              Re: Looping issues

              brochu121@gmail .com a écrit :
              What I am trying to do is compare two files to each other.
              >
              If the 2nd file contains the same line the first file contains, I want
              to print it. I wrote up the following code:
              >
              >
              >
              correct_setting s = open("C:\Python 25\Scripts\Outp ut
              \correct_settin gs.txt","r")
              current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
              >
              for line in correct_setting s:
              for val in current_setting s:
              if val == line:
              print line + " found."
              >
              >
              correct_setting s.close()
              current_setting s.close()
              >
              >
              For some reason this only looks at the first line of the
              correct_setting s.txt file.
              Not quite. It really go thru the whole file. But it only enter the inner
              loop for the first line. Then the file iterator is exhausted, and the
              other iterations are noop. You can verify this by adding a couple print
              statements - one in the outer loop and one in the inner one.
              Any ideas as to how i can loop through each
              line of the correct_setting s file instead of just looking at the first?
              >
              The good question is "how to loop thru the lines of *current_*setti ngs
              for each line of correct_setting s". The answer is : don't use the file
              iterator here, read the whole file in memory. Cf Larry's post for a howto.

              Comment

              • hlubenow

                #8
                Re: Looping issues

                brochu121@gmail .com wrote:
                On Apr 5, 2:18 pm, "anglozaxxo n" <anglozax...@gm ail.comwrote:
                >On Apr 5, 2:01 pm, brochu...@gmail .com wrote:
                >>
                >>
                >>
                What I am trying to do is compare two files to each other.
                >>
                If the 2nd file contains the same line the first file contains, I want
                to print it. I wrote up the following code:
                >>
                correct_setting s = open("C:\Python 25\Scripts\Outp ut
                \correct_settin gs.txt","r")
                current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
                >>
                for line in correct_setting s:
                for val in current_setting s:
                if val == line:
                print line + " found."
                >>
                correct_setting s.close()
                current_setting s.close()
                >>
                For some reason this only looks at the first line of the
                correct_setting s.txt file. Any ideas as to how i can loop through each
                line of the correct_setting s file instead of just looking at the first?
                >>
                >Instead of "for line in correct_setting s", try "for line in
                >correct_settin gs.readlines()" .
                >
                That Still didnt fix it. Same output
                Try this:

                correct_setting s = file("C:\Python 25\Scripts\Outp ut\correct_sett ings.txt",
                "r")
                current_setting s = file("C:\Python 25\Scripts\Outp ut\output.txt", "r")

                a = correct_setting s.readlines()
                b = current_setting s.readlines()

                correct_setting s.close()
                current_setting s.close()

                for line in a:
                for val in b:
                if val == line:
                print line + " found."

                Comment

                • Peter Otten

                  #9
                  Re: Looping issues

                  Larry Bates wrote:
                  If the files aren't terribly large (not tested):
                  >
                  correct_lines=o pen(r"C:\Python 25\Scripts\Outp ut" \
                                      "\correct_setti ngs.txt", "r").readlines( )
                  >
                  current_lines=o pen(r"C:\Python 25\Scripts\Outp ut\output.txt",
                                      "r").readlines( )
                  >
                  for line in current_setting s:
                      if line in correct_lines:
                          print line + " found"
                  You only have to read the "inner" file into memory, and a set is more
                  efficient than a list here:

                  current_setting s = open(...)
                  correct_setting s = set(open(...))

                  for line in current_setting s:
                  if line in correct_setting s:
                  print line.rstrip(), "found"

                  Of course your suggestion of difflib is spot-on.

                  Peter

                  Comment

                  • thebjorn

                    #10
                    Re: Looping issues

                    On Apr 5, 8:01 pm, brochu...@gmail .com wrote:
                    What I am trying to do is compare two files to each other.
                    >
                    If the 2nd file contains the same line the first file contains, I want
                    to print it. I wrote up the following code:
                    >
                    correct_setting s = open("C:\Python 25\Scripts\Outp ut
                    \correct_settin gs.txt","r")
                    current_setting s = open("C:\Python 25\Scripts\Outp ut\output.txt", "r")
                    >
                    for line in correct_setting s:
                    for val in current_setting s:
                    if val == line:
                    print line + " found."
                    >
                    correct_setting s.close()
                    current_setting s.close()
                    >
                    For some reason this only looks at the first line of the
                    correct_setting s.txt file. Any ideas as to how i can loop through each
                    line of the correct_setting s file instead of just looking at the first?
                    I'm not entirely sure I understand what you're trying to do, but in
                    case you're trying to walk through the two files in lockstep printing
                    the lines that correspond, here's a way to do that:

                    # note the r'..' syntax
                    correct = open(r'c:\pytho n25\scripts\out put\correct_set tings.txt')
                    current = open(r'c:\pytho n25\scripts\out put\output.txt' )

                    for correct_line, current_line in zip(correct, current):
                    if correct_line == current_line:
                    print correct_line, 'found.'

                    correct.close()
                    current.close()

                    hth,
                    -- bjorn

                    Comment

                    Working...