By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

How to compare files

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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...

Jul 19 '05 #2

P: n/a
Lad wrote:
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

--
// Today's Oblique Strategy ( Brian Eno/Peter Schmidt):
// Change instrument roles
// Brett g Porter * Bg******@acm.org

Jul 19 '05 #3

P: n/a
On Friday 01 July 2005 04:39 am, Lad wrote:
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).

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 19 '05 #4

P: n/a
Lad wrote:
Hi,
What is the best method for comparing two files by words?


try the difflib module:
http://www.python.org/doc/2.4.1/lib/module-difflib.html
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.