Python Forum
get only additions in difflib.unified_diff
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get only additions in difflib.unified_diff
#1
Im using difflib to mimic git diff between two files in python. It works as expected and as git diff does it. However i am wondering if you can only get the additions of the different files?

What defines additional VS removals?

Im using it in this manner:
if args['diff']:
    old = open(args['output'])
    new = output_str
    for line in difflib.unified_diff(old.readlines(), new.splitlines(True), fromfile=old.name,           tofile='current', lineterm='\n'):
        print(line)
    old.close()
an example snippet output would be
Output:
-9911 Church Creek Rd Lindley, NY 14858 -Thursday May 04, 2017 - Saturday May 06, 2017 Time: 7:00AM - 4:00PM -Crafts, tools, kitchen items, yarn, power washer, ab roller, fishing poles too much to list. +638 River Rd Lindley, NY 14858 +Saturday May 06, 2017 - Saturday May 13, 2017 Time: 9:00AM +Everything must go!!!!!
is what i current get, when i only want to see

Quote:+638 River Rd Lindley, NY 14858

+Saturday May 06, 2017 - Saturday May 13, 2017 Time: 9:00AM

+Everything must go!!!!!


I dont see any additional arguments to give such restrictions
Is this possible with just difflib or would it require string parsing to do?
Recommended Tutorials:
Reply
#2
I notice you're using splitlines, which removes trailing \n's. There's this note in the docs:

Documentation Wrote:For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free.

Could that have something to do with it?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
im not worried about the newlines, just obtaining only additions VS deletions.
Recommended Tutorials:
Reply
#4
(May-08-2017, 02:56 AM)metulburr Wrote: im not worried about the newlines, just obtaining only additions VS deletions.
I don't think you have such an option - just filter out every line that doesn't start with +[space]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant use difflib with islice? gonksoup 2 372 Jan-22-2024, 01:07 PM
Last Post: deanhystad
  [difflib] read files with SequenceMatcher JamieVanCadsand 3 4,739 Sep-15-2017, 09:15 AM
Last Post: JamieVanCadsand

Forum Jump:

User Panel Messages

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