Questions tagged [difflib]

A python module, provides tools for computing and working with differences between sequences, especially useful for comparing text. Includes functions that produce reports using several common difference formats.

A python module which provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce difference information in various formats, including HTML and context and unified diffs.

271 questions
4
votes
3 answers

How to compare two models to be rendered with Markdown using Django?

What is the best way to check for changes (edited/added/deleted text) in a post between two post's versions (original and edited one)? I am using Markdown so I am not sure if using difflib.HtmlDiff is a good idea. My goal is to mark with a green…
Pompeyo
  • 1,319
  • 3
  • 17
  • 40
3
votes
2 answers

difflib returns different ratio depending on order of sequences

Does anyone know why these two return different ratios. >>> import difflib >>> difflib.SequenceMatcher(None, '10101789', '11426089').ratio() 0.5 >>> difflib.SequenceMatcher(None, '11426089', '10101789').ratio() 0.625
iiijjjiii
  • 33
  • 3
3
votes
1 answer

How to highlight more than two characters per line in difflibs html output

I am using difflib.HtmlDiff to compare two files. I want the differences to be highlighted in the outputted html. This already works when there are a maximum of two different chars in one line: a = "2.000" b = "2.120" But when there are more…
Lars Bilke
  • 4,228
  • 5
  • 37
  • 58
3
votes
3 answers

Python difflib with regular expressions

Can I use regular expressions in difflib? Specifically, I'd like to do: difflib.context_diff(actual, gold) Where actual is: [master 92a406f] file modified and gold is: \[master \w{7}\] file modified
Jonathan
  • 83
  • 1
  • 5
3
votes
1 answer

Using the difflib.HtmlDiff class - showing single chars

I am using the difflib.HtmlDiff class, calling the function using two sets of text (HTML from websites), however when it makes the table html_diff = difflib.HtmlDiff() print html_diff.make_table(previous_contents, fetch_url.page_contents) however…
Wizzard
  • 11,849
  • 19
  • 60
  • 97
3
votes
1 answer

Group Unique Values on Unique Value with most occurence Python

Below is a sample of my df name A S BITO A S KIGEL A S NATURENERGI A S NATURENERGIE A S NATURENERGIE A S P BU SERVICE POWER P A S P BU SERVICE POWER P A S P BU SERVICE POWER PETER GMBH A S P GMBH A RESE LAND A RITTER WITH SA A RITTER WITH SA …
Annis15
  • 308
  • 1
  • 11
3
votes
3 answers

auto-correct the words from the list in python

I want to auto-correct the words which are in my list. Say I have a list kw = ['tiger','lion','elephant','black cat','dog'] I want to check if these words appeared in my sentence. If they are wrongly spelled I want to correct them. I don't intend…
Sociopath
  • 11,667
  • 16
  • 38
  • 61
3
votes
3 answers

Python sequence matcher with custom matching function

I have two lists and I want to find the matching elements using python difflib/sequence matcher, and it goes like this: from difflib import SequenceMatcher def match_seq(list1,list2): output=[] s = SequenceMatcher(None, list1, list2) …
hmghaly
  • 1,467
  • 2
  • 18
  • 40
3
votes
1 answer

Comparing lists in python by difflib modul

I am trying out the difflib library. I have two lists: L_1 and L_2 containing strings. I want to know, if those sequences are similar (order is not important). L_1 = ["Bob", "Mary", "Hans"] L_2 = ["Bob", "Marie", "Háns"] should be ok. But L_1 =…
Aufwind
  • 22,034
  • 33
  • 94
  • 149
3
votes
1 answer

Finding Close String Matches - valuing sub string word matches higher

I'm trying to find close string matches (context - searching for a discord user from user input). Atm, I'm trying out the difflib. It works ok, but seems to return some funny results sometimes. Eg. if someone's name contains a word, searching that…
Shuri2060
  • 548
  • 5
  • 18
3
votes
1 answer

How does Python 3.6 SequenceMatcher().get_matching_blocks() work?

I am trying to use SequenceMatcher.ratio() to get the similarity of two strings: "86418648" and "86488648": >>> SequenceMatcher(None,"86418648","86488648").ratio() 0.5 The ratio returned is 0.5, which is much lower than I expected because there is…
Jessie
  • 31
  • 4
3
votes
2 answers

Get standard gnu diff output from Python's difflib?

Is there any way to get the following output (especially the 1,4c1,4 syntax) from Python's difflib? diff foo baz 1,4c1,4 < 'asdf' < 'asdf' < 'asdf' < 'asdf' --- > asdf > asdf > asdf > asdf
kev
  • 7,033
  • 12
  • 46
  • 83
3
votes
1 answer

How to understand/use the Python difflib output?

I am trying to make comprehensive diff that compares command line output of two programs. I used difflib and came up with this code: from difflib import Differ from pprint import pprint import sys def readable_whitespace(line): return…
3
votes
1 answer

Why does unified_diff method from the difflib library in Python leave out some characters?

I am trying to check for differences between lines. This is my code: from difflib import unified_diff s1 = ['a', 'b', 'c', 'd', 'e', 'f'] s2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n'] for line in unified_diff(s1, s2): print…
Shivani
  • 65
  • 9
3
votes
2 answers

How to fuzzy match movie titles with difflib and pandas?

I have 2 lists of potentially overlapping movie titles, but possibly written in a different form. They are in 2 different dataframes from pandas. So I have tried to use the map() function with the fuzzywuzzy library like so: df1.title.map(lambda x:…
Bastian
  • 4,813
  • 10
  • 39
  • 61
1 2
3
18 19