On Jan 18, 12:08 pm, Madhur <[EMAIL PROTECTED]> wrote: > On Jan 18, 2:37 pm, Chris <[EMAIL PROTECTED]> wrote: > > > > > On Jan 18, 11:23 am, Madhur <[EMAIL PROTECTED]> wrote: > > > > I would like to know the best way of generating filter of two files > > > based upon the following condition > > > > I have two files. Contents of the first file is > > > > File 1 > > > abc def hij > > > asd sss lmn > > > hig pqr mno > > > > File 2 > > > > jih def asd > > > poi iuu wer > > > wer pqr jjj > > > > I would like have the output as > > > Output > > > > File1 > > > asd sss lmn > > > File2 > > > poi iuu wer > > > > Basically I want to compare the two files based on second column. If > > > the second > > > column matches on both the files do not print anything, else if there > > > is no matc > > > h in for the second column for first file in second file then print it > > > under Fil > > > e1 header, else if there is no match for the second column for second > > > file in fi > > > rst file print it under File2 header. > > > > Thankyou > > > Madhur > > > file1 = open('file1.txt','rb') > > file2 = open('file2.txt','rb') > > > file1_line = file1.next() > > file2_line = file2.next() > > > while file1_line and file2_line: > > try: > > f1_col2 = file1_line.split(' ')[1] > > except IndexError: > > print 'Not enough delimiters in line.' > > try: > > f2_col2 = file2_line.split(' ')[2] > > except IndexError: > > print 'Not enough delimiters in line.' > > > if f1_col2 != f2_col2: > > outfile_data_to_relevant_files() > > > file1_line = file1.next() > > file2_line = file2.next() > > > HTH > > Chris > > If the files2 is unordered, then the above logic does not work. How to > takle it?
Take a look at *nix's sort command, it can also sort based on a key -- http://mail.python.org/mailman/listinfo/python-list