473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_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 '07 #1
9 1222
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_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()".

Apr 5 '07 #2
On Apr 5, 2:18 pm, "anglozaxxon" <anglozax...@gmail.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_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 '07 #3
br*******@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_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\ou tput.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\ou tput.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
Apr 5 '07 #4
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_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...)
Apr 5 '07 #5
On Apr 5, 2:27 pm, Larry Bates <larry.ba...@websafe.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_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\ou tput.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\ou tput.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

Apr 5 '07 #6
br*******@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_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.
Apr 5 '07 #7
br*******@gmail.com wrote:
On Apr 5, 2:18 pm, "anglozaxxon" <anglozax...@gmail.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_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."

Apr 5 '07 #8
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\ou tput.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

Apr 6 '07 #9
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_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

Apr 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
1
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
2
by: randy1200 | last post by:
My hope is that somebody has a thought on this, or can point to toward an article that's useful. I have the following: DataSet ds = new DataSet(); ds.ReadXml("MyData.xml"); Each MainTable...
4
by: musosdev | last post by:
Hi I need to extend the contents of the src element of the <link relcss tags in the <headsection of my masterpage. From code, how can I access the <link elements within the <head? Cheers
0
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
20
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
4
by: wbosw | last post by:
I'm trying to take a word (stEAdy) and find the index positions of the uppercase characters(2,3) in the word. I have them stored in an array (positionarray). Then I reverse the word and set all...
1
by: gwenky | last post by:
Hello, so I was working this out yesterday and I am working on a function (w/o using arrays) that moves through the digits of a user inputted number and finds max/min ...this is the max option and it...
2
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.