Advertisement
stream13

Find_Common_Strings

Jan 13th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import os
  5.  
  6. # Global variables
  7. file_names = []
  8. file_contents = []
  9. line = "" #temporary variable for line
  10.  
  11.  
  12. def help():
  13.     print "USAGE: common_id FILE_1 FILE_2 ... FILE_N\n"
  14.     return None
  15.        
  16. def find_common(id1, id2):
  17.     count = 0
  18.     for i in xrange(len(file_contents[id1])):
  19.         for ii in xrange(len(file_contents[id2])):
  20.             if(file_contents[id1][i] == file_contents[id2][ii]):
  21.                 count += 1
  22.     # print "id1 = %d, id2 = %d, count = %d" % (id1, id2, count)
  23.     return count
  24.  
  25.  
  26. # Logics itself
  27. if (len(sys.argv) < 3 or sys.argv[1] == "-h"):
  28.         help()
  29.         exit()
  30.  
  31. #Check filenames
  32. for i in xrange(1, len(sys.argv)):
  33.     print "Filename = %s" % (sys.argv[i])
  34.     if( os.path.exists(sys.argv[i]) and os.path.isfile(sys.argv[i]) ):
  35.         file_names.append(sys.argv[i])
  36.     else:
  37.         print "ERROR: File %s not exists or is inaccessible!" % (sys.argv[i])
  38.         print "Exiting..."
  39.         exit()
  40.  
  41. print "filenames len = %d" % (len(file_names))
  42.  
  43. #Read files
  44. for f_id in xrange(len(file_names)):
  45.     file_contents.append([])
  46.     pfile = open(file_names[f_id], 'r')
  47.     line = pfile.readline()
  48.     # t_id = 1
  49.     while (line != None) and (line != "" ):
  50.         file_contents[f_id].append(line)
  51.         line = pfile.readline()
  52.     pfile.close()
  53.     # print "%d" % (t_id)
  54.     # t_id = t_id + 1
  55.     # print "File = %s; len = %d" % ( file_names[f_id], len(file_contents[f_id]) )
  56.  
  57. # print "Contents = %d" % (len(file_contents))
  58.  
  59. # Another global variable
  60. out_table = [["" for x in xrange(len(file_names) + 1)] for x in xrange(len(file_names) + 1)]
  61.  
  62. out_table[0][0] = "\\"
  63. out_str = ""
  64. tmp_int = 0
  65. tmp_str = ""
  66. for ix in xrange(len(file_names)):
  67.     out_table[ix+1][0] = file_names[ix] # horizontal names
  68.     out_table[0][ix+1] = file_names[ix] # vertical   names
  69.  
  70.     for iy in xrange(len(file_names)):
  71.         if out_table[ix+1][iy+1] == "" :
  72.             if ix == iy:
  73.                 out_table[ix+1][iy+1] = "\\"
  74.             else:
  75.                 out_table[ix+1][iy+1] = str(find_common(ix,iy))
  76.                 out_table[iy+1][ix+1] = out_table[ix+1][iy+1] # copying identical values
  77.  
  78. for ix in xrange(len(out_table)):
  79.     for iy in xrange(len(out_table[ix])):
  80.         out_str += out_table[ix][iy]
  81.         if( iy < (len(out_table) - 1)):
  82.             out_str += ", "
  83.     out_str += "\n"
  84.  
  85. print out_str
  86.  
  87. pfile = open('out.csv', 'w')
  88. pfile.write(out_str)
  89. pfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement