3.3.3 differObject
The differ object is a full-text comparison, and then the list increases, decreases, or is the same position.
Class Difflib. Differ (Linejunk=none, Charjunk=none)
Constructs a differ object,linejunk and charjunk are optional filter functions, if not set, default to None .
Linejunk is a function object that can filter unwanted string rows and returns True when not needed .
Charjunk is a function object that can filter unwanted characters and returns True when not needed .
Compare (A, B)
Compares two sequences, generating an added or deleted text description.
3.3.4UsedifferObject
The differ object is primarily used to compare the differences between text, and then generates a string returned by a certain rule for the differential text.
Compare two simple strings
Example:
#python3.4.3
Import Difflib
From Pprint import Pprint
S1 = ' ABCD 123 321 '
S2 = ' abcd abcd 123321 '
Print (' S1 = ', S1)
Print (' s2 = ', s2)
Print (' S1 = = S2 ', S1 = = s2)
Print (")
diff = Difflib. Differ ()
Print (diff)
L = List (Diff.compare (S1, S2))
Pprint (L)
The resulting output is as follows:
S1 = ABCD 123 321
S2 = ABCD ABCD 123321
S1 = = S2 False
<difflib. Differ object at 0x0298b4d0>
[' + A ',
' + B ',
' + C ',
' + d ',
‘ ‘,
' A ',
' B ',
' C ',
' d ',
‘ ‘,
' 1 ',
' 2 ',
' 3 ',
‘- ‘,
' 3 ',
' 2 ',
' 1 ']
As you can see from this example, you first create the differ object and then call the function compare to compare the strings S1 and S2 , and finally generate a list of the results List return, by Pprint to print out the output, which is used here Pprint printing is because the output is easier to see clearly, each line is increased or decreased, is kept constant, and so on. There is a plus sign in front of the result, no sign is the same, minus means delete.
Compare multiple lines of text
For text files to compare, in the need for more applications, such as the source of version control to compare two different versions of the changes, so you can know each version of what code changed, such as in the cloud service to save the different text, can only keep the increase of the data, which can reduce the cost of storage, You can also revert to a previous version.
Example:
#python3.4.3
Import Difflib
From Pprint import Pprint
Text1 = ' 1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
". Splitlines (Keepends=true)
Text2 = ' 1. Beautiful is better than ugly.
3. Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.
". Splitlines (Keepends=true)
diff = Difflib. Differ ()
Print (diff)
L = List (Diff.compare (Text1, Text2))
Pprint (L)
The resulting output is as follows:
<difflib. Differ object at 0x029eb4b0>
[' 1. Beautiful is better than ugly.\n ',
'-2. Explicit is better than implicit.\n ',
'-3. Simple is better than complex.\n ',
' + 3. Simple is better than complex.\n ',
‘? ++\n ',
'-4. Complex is better than complicated.\n ',
‘? ^----^\n ',
' + 4. Complicated is better than complex.\n ',
‘? ++++ ^ ^\n ',
' + 5. Flat is better than nested.\n ',
‘ ‘]
See this display in more detail from the results, and the character differences in each line are already marked.
Cai Junsheng No.: Shenzhencai Shenzhen
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
3.3.3 differ Object