Looping issues

Sick Monkey sickcodemonkey at gmail.com
Thu Apr 5 16:10:47 EDT 2007


Ooops, Sorry I left .lower() out.

################################################################
def fopen(fname):
    try:
        return open(fname, 'U')
    except IOError, detail:
        return fail("couldn't open " + fname + ": " + str(detail))
################################################################
f1 = fopen('file1.txt')
a = f1.readlines(); f1.close()
d1 = {}
for c in a:
      d1[c.lower()] = None
      #print d1
f2 = fopen('file2.txt')
data = f2.readlines(); f2.close()
for line in data:
      if d1.has_key( line.lower() ):
          print line


On 4/5/07, Sick Monkey <sickcodemonkey at gmail.com> wrote:
>
> Try this.
> NOTE:  The script below will compare lines of both files (even hidden
> characters such as \n)...
>
> ################################################################
> def fopen(fname):
>     try:
>         return open(fname, 'U')
>     except IOError, detail:
>         return fail("couldn't open " + fname + ": " + str(detail))
> ################################################################
> f1 = fopen('file1.txt ')
> a = f1.readlines(); f1.close()
> d1 = {}
> for c in a:
>       d1[c.lower()] = None
>       #print d1
> f2 = fopen('file2.txt')
> data = f2.readlines(); f2.close()
> for line in data:
>       if d1.has_key( line ):
>           print line
> ################################################################
>
> wdh
>
> On 5 Apr 2007 11:01:09 -0700, brochu121 at gmail.com <brochu121 at 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?
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070405/e0a4b313/attachment.html>


More information about the Python-list mailing list