Looping issues

Discussion in 'Python' started by brochu121@gmail.com, Apr 5, 2007.

  1. Guest

    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_settings = open("C:\Python25\Scripts\Output
    \correct_settings.txt","r")
    current_settings = open("C:\Python25\Scripts\Output\output.txt","r")

    for line in correct_settings:
    for val in current_settings:
    if val == line:
    print line + " found."


    correct_settings.close()
    current_settings.close()


    For some reason this only looks at the first line of the
    correct_settings.txt file. Any ideas as to how i can loop through each
    line of the correct_settings file instead of just looking at the first?
     
    , Apr 5, 2007
    #1
    1. Advertising

  2. anglozaxxon Guest

    On Apr 5, 2:01 pm, 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_settings = open("C:\Python25\Scripts\Output
    > \correct_settings.txt","r")
    > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")
    >
    > for line in correct_settings:
    > for val in current_settings:
    > if val == line:
    > print line + " found."
    >
    > correct_settings.close()
    > current_settings.close()
    >
    > For some reason this only looks at the first line of the
    > correct_settings.txt file. Any ideas as to how i can loop through each
    > line of the correct_settings file instead of just looking at the first?


    Instead of "for line in correct_settings", try "for line in
    correct_settings.readlines()".
     
    anglozaxxon, Apr 5, 2007
    #2
    1. Advertising

  3. Guest

    On Apr 5, 2:18 pm, "anglozaxxon" <> wrote:
    > On Apr 5, 2:01 pm, 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_settings = open("C:\Python25\Scripts\Output
    > > \correct_settings.txt","r")
    > > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")

    >
    > > for line in correct_settings:
    > > for val in current_settings:
    > > if val == line:
    > > print line + " found."

    >
    > > correct_settings.close()
    > > current_settings.close()

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

    >
    > Instead of "for line in correct_settings", try "for line in
    > correct_settings.readlines()".


    That Still didnt fix it. Same output
     
    , Apr 5, 2007
    #3
  4. Larry Bates Guest

    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_settings = open("C:\Python25\Scripts\Output
    > \correct_settings.txt","r")
    > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")
    >
    > for line in correct_settings:
    > for val in current_settings:
    > if val == line:
    > print line + " found."
    >
    >
    > correct_settings.close()
    > current_settings.close()
    >
    >
    > For some reason this only looks at the first line of the
    > correct_settings.txt file. Any ideas as to how i can loop through each
    > line of the correct_settings file instead of just looking at the first?
    >

    If the files aren't terribly large (not tested):

    correct_lines=open(r"C:\Python25\Scripts\Output" \
    "\correct_settings.txt", "r").readlines()

    current_lines=open(r"C:\Python25\Scripts\Output\output.txt",
    "r").readlines()

    for line in current_settings:
    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=open(r"C:\Python25\Scripts\Output" \
    "\correct_settings.txt", "r").readlines()

    current_lines=open(r"C:\Python25\Scripts\Output\output.txt",
    "r").readlines()

    delta=difflib.unified_diff(correct_lines, current_lines)
    diffs=''.join(delta)

    print diffs

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

    -Larry
     
    Larry Bates, Apr 5, 2007
    #4
  5. anglozaxxon Guest

    On Apr 5, 2:27 pm, Larry Bates <> wrote:
    > 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_settings = open("C:\Python25\Scripts\Output
    > > \correct_settings.txt","r")
    > > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")

    >
    > > for line in correct_settings:
    > > for val in current_settings:
    > > if val == line:
    > > print line + " found."

    >
    > > correct_settings.close()
    > > current_settings.close()

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

    >
    > If the files aren't terribly large (not tested):
    >
    > correct_lines=open(r"C:\Python25\Scripts\Output" \
    > "\correct_settings.txt", "r").readlines()
    >
    > current_lines=open(r"C:\Python25\Scripts\Output\output.txt",
    > "r").readlines()
    >
    > for line in current_settings:
    > 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=open(r"C:\Python25\Scripts\Output" \
    > "\correct_settings.txt", "r").readlines()
    >
    > current_lines=open(r"C:\Python25\Scripts\Output\output.txt",
    > "r").readlines()
    >
    > delta=difflib.unified_diff(correct_lines, current_lines)
    > diffs=''.join(delta)
    >
    > 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_settings.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_settings.read().replace('\\','\\\\'). 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
     
    anglozaxxon, Apr 5, 2007
    #5
  6. anglozaxxon a écrit :
    > On Apr 5, 2:01 pm, wrote:
    >

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

    >
    >
    > Instead of "for line in correct_settings", try "for line in
    > correct_settings.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...)
     
    Bruno Desthuilliers, Apr 5, 2007
    #6
  7. 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_settings = open("C:\Python25\Scripts\Output
    > \correct_settings.txt","r")
    > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")
    >
    > for line in correct_settings:
    > for val in current_settings:
    > if val == line:
    > print line + " found."
    >
    >
    > correct_settings.close()
    > current_settings.close()
    >
    >
    > For some reason this only looks at the first line of the
    > correct_settings.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_settings file instead of just looking at the first?
    >

    The good question is "how to loop thru the lines of *current_*settings
    for each line of correct_settings". The answer is : don't use the file
    iterator here, read the whole file in memory. Cf Larry's post for a howto.
     
    Bruno Desthuilliers, Apr 5, 2007
    #7
  8. hlubenow Guest

    wrote:

    > On Apr 5, 2:18 pm, "anglozaxxon" <> wrote:
    >> On Apr 5, 2:01 pm, 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_settings = open("C:\Python25\Scripts\Output
    >> > \correct_settings.txt","r")
    >> > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")

    >>
    >> > for line in correct_settings:
    >> > for val in current_settings:
    >> > if val == line:
    >> > print line + " found."

    >>
    >> > correct_settings.close()
    >> > current_settings.close()

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

    >>
    >> Instead of "for line in correct_settings", try "for line in
    >> correct_settings.readlines()".

    >
    > That Still didnt fix it. Same output


    Try this:

    correct_settings = file("C:\Python25\Scripts\Output\correct_settings.txt",
    "r")
    current_settings = file("C:\Python25\Scripts\Output\output.txt", "r")

    a = correct_settings.readlines()
    b = current_settings.readlines()

    correct_settings.close()
    current_settings.close()

    for line in a:
    for val in b:
    if val == line:
    print line + " found."
     
    hlubenow, Apr 5, 2007
    #8
  9. Peter Otten Guest

    Larry Bates wrote:

    > If the files aren't terribly large (not tested):
    >
    > correct_lines=open(r"C:\Python25\Scripts\Output" \
    >                     "\correct_settings.txt", "r").readlines()
    >
    > current_lines=open(r"C:\Python25\Scripts\Output\output.txt",
    >                     "r").readlines()
    >
    > for line in current_settings:
    >     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_settings = open(...)
    correct_settings = set(open(...))

    for line in current_settings:
    if line in correct_settings:
    print line.rstrip(), "found"

    Of course your suggestion of difflib is spot-on.

    Peter
     
    Peter Otten, Apr 6, 2007
    #9
  10. thebjorn Guest

    On Apr 5, 8:01 pm, 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_settings = open("C:\Python25\Scripts\Output
    > \correct_settings.txt","r")
    > current_settings = open("C:\Python25\Scripts\Output\output.txt","r")
    >
    > for line in correct_settings:
    > for val in current_settings:
    > if val == line:
    > print line + " found."
    >
    > correct_settings.close()
    > current_settings.close()
    >
    > For some reason this only looks at the first line of the
    > correct_settings.txt file. Any ideas as to how i can loop through each
    > line of the correct_settings 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:\python25\scripts\output\correct_settings.txt')
    current = open(r'c:\python25\scripts\output\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
     
    thebjorn, Apr 6, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mothra
    Replies:
    3
    Views:
    438
  2. CJ
    Replies:
    0
    Views:
    1,836
  3. Aemca
    Replies:
    1
    Views:
    575
    Aemca
    Jul 22, 2003
  4. Klauer
    Replies:
    0
    Views:
    436
    Klauer
    Jul 20, 2007
  5. Replies:
    5
    Views:
    288
Loading...

Share This Page