Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference Between 2 files
#1
Hi

I've a question to compare 2 files.

My first file 'file_new' contain:
Output:
1 2 3 5 6 7
My second file 'file_old' contains:
Output:
1 2 3 4 5 6
I want to compare these two files.
My file_new contain "reality" now.

So my script is
#!/usr/bin/env python3
import difflib

file_old = "/tmp/file_old"
file_new = "/tmp/file_new"

diff = difflib.ndiff(open(file_old).readlines(),open(file_new).readlines())
print (''.join(diff))
Result is:
Output:
1 2 3 - 4 5 6 + 7
Is there any mean to retrieve simply - and + values?
- To put them on a new file?
- To put them in an array / temporary json?

I'm doing some tests and reading docs but for the moment not concluant.

Thanks for ideas and help

Alex
Reply
#2
maybe difflib:
https://docs.python.org/3.8/library/difflib.html
https://pymotw.com/3/difflib/index.html
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
One should make unambiguous what does 'difference' means.

For example, using difflib.Differ() one will get differences as follows (provided data in files named diff_1.txt and diff_2.txt respectively):

with open('diff_1.txt', 'r') as x, open('diff_2.txt', 'r') as y:
    diff = difflib.Differ().compare(x.readlines(), y.readlines())
    different_rows = (value.strip() for value in diff if value.startswith(('+', '-')))
    print(*different_rows, sep='\n')


Output:
+ 4 + 6 - 6 - 7
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Simply use filter()
import difflib
import io

f_new = io.StringIO('''\
1
2
3
5
6
7
''')

f_old = io.StringIO('''\
1
2
3
4
5
6
''')

def my_ndiff(*args):
    return filter(lambda x: not x.startswith(' '), difflib.ndiff(*args))

if __name__ == '__main__':
    diff = my_ndiff(f_old.readlines(), f_new.readlines())
    print(''.join(diff))
Output:
- 4 + 7
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python 3 find difference between 2 files pd007 2 456 May-22-2020, 01:16 AM
Last Post: Larz60+
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 718 May-22-2019, 07:20 AM
Last Post: buran
  Match CSV files for difference Cuz 4 1,287 Dec-18-2018, 02:16 PM
Last Post: Cuz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020