How to compare files

L

Lad

Hi,
What is the best method for comparing two files by words?
I was thinking about reading files by words and compare them but a word
in one file can be linked with a new line character ( \n)
and this '\n' will cause that the words will considered to be
different( eventhough without '\n' are the same)

Thanks for help.
LAd.
 
D

dberlin

you could always just remove those special characters (\n \t ..),
remove spaces, read both files and compare string. I don't this is the
best way of doing this... but maybe a combination of this way and yours
will be efficient enough - remove all problematic characters and then
compare line by line.

good luck...
 
B

Brett g Porter

Lad said:
Hi,
What is the best method for comparing two files by words?
I was thinking about reading files by words and compare them but a word
in one file can be linked with a new line character ( \n)
and this '\n' will cause that the words will considered to be
different( eventhough without '\n' are the same)

Thanks for help.
LAd.
Have you looked at the difflib module that comes with Python?

http://docs.python.org/lib/module-difflib.html
 
T

Terry Hancock

Hi,
What is the best method for comparing two files by words?
I was thinking about reading files by words and compare them but a word
in one file can be linked with a new line character ( \n)
and this '\n' will cause that the words will considered to be
different( eventhough without '\n' are the same)

Use the split from re instead of the string method, and
include all the non-word characters (including '\n') as
whitespace characters. Then you'll have only words
(I think you can also wind up with empty strings, which
you can strip out pretty easily).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top