file comparison

Dave Angel davea at ieee.org
Mon Aug 3 23:23:38 EDT 2009


learner learner wrote:
> Firstly thanks for showing the interest. I shall elobarate more on the
> problem:
>
> file-1.txt
> --------------
> hai
> how
> r
> u
>
> file-2.txt
> ---------------
> r
> hai
> u
>
>
> The two files have some lines in common.
>
> For eg: File-1.txt-first line-"hai" does not match with File-2.txt-first
> line-"r" but surely matches with File-2.txt -second
>
> line -"hai". On having an iterative search the file-1.txt should finally be
> left out with mismatched string "how", which is
>
> to be dumped into a third file say file3.txt or rewritten to
> file-1.txt/file-2.txt with this single word "how" in line 1.
>
> some facts to remember:
> ------------------------
>
> the srings in the files will be same including case sentiveness if at all
> present, else will be pronounced as a mismatch.
>
> this is bascically an iterative string search amongst the files and
> isolating the mismatched strings into a text file
>
>   
(By replying off-list, you are ignoring the intent of public mailing 
lists.  So I'm copying your message to the list, along with my response.)

As I said before:

If the text files are each a list of items, with order and duplication 
irrelevant, then your best bet is probably to build a set from each, and 
difference the sets.

Sets are case-sensitive, and your example above indicates that order is 
at least partially irrelevant to you.

So read each file, converting the lines into a set.  Then do a set 
difference of those two sets.  Then build a list from the result, and 
write that to a file.  If it doesn't work, post the code you tried, 
along with sample data and expected output for that sample data.

DaveA





More information about the Python-list mailing list