Retrieving Line Numbers That Have Been Changed

Given the two text files A, B, what is an easy way to get line numbers of lines in B not present in A? I see difflib there, but I don’t see the interface for getting line numbers

+4
source share
2 answers

difflib can give you what you need. Suppose:

a.txt

this is a bunch of lines 

b.txt

 this is a different bunch of other lines 

The code is as follows:

 import difflib fileA = open("a.txt", "rt").readlines() fileB = open("b.txt", "rt").readlines() d = difflib.Differ() diffs = d.compare(fileA, fileB) lineNum = 0 for line in diffs: # split off the code code = line[:2] # if the line is in both files or just b, increment the line number. if code in (" ", "+ "): lineNum += 1 # if this line is only in b, print the line number and the text on the line if code == "+ ": print "%d: %s" % (lineNum, line[2:].strip()) 

gives a conclusion, for example:

 bgporter@varese ~/temp:python diffy.py 4: different 7: other 

You will also want to look at the difflib "? " Code and see how you want to handle this.

(also in real code, would you like to use context managers to make sure the files are closed, etc. etc.)

+11
source

Poor person's decision:

 with open('A.txt') as f: linesA = f.readlines() with open('B.txt') as f: linesB = f.readlines() print [k for k, v in enumerate(linesB) if not v in linesA] 
+1
source

Source: https://habr.com/ru/post/1399077/


All Articles