1

I need to read a lines from two files. Extract data from the first line of file A and compare it with each line in file B. When I'm done with file B, I have to read next line in file A and compare it with all lines in file B, and so on ans so forth. I tried to use file.readline(), but when I use a while, it only prints the last line

import sys
def ReadFile():
  name1='RoutingTable.txt'
  try:
    arch_table= open(name1,'r')
  except IOError:
      print 'Cannot Open', name1   
      sys.exit()

  while True:
   route=arch_table.readline()
   print route
      if not route:
            break
      pass
4

1 回答 1

1

让你开始的东西:

$ cat f1
kalle
trazan
apanzon

$ cat f2
dipsy
poo
laalaa
trazan


$ cat two.py
#!/usr/bin/env python

with open('f1') as fd1:
    for line in fd1:
        with open('f2') as fd2:
            for other_line in fd2:
                if line == other_line:
                    print line

输出:

$ ./two.py 
trazan
于 2013-02-02T20:18:53.007 回答