arsenal88
arsenal88

Reputation: 1160

String comparison not complete match

I have a string, for example:

my_string = "Man Utd (Supernova) Esports"

And I'm trying to access API data that has this down as Manchester United (Supernova) Esports

Now I can just do:

If 'Man' in `Manchester United (Supernova) Esports:
   do something()

But this is a very simple string with a simple answer. There are a whole host of other string naming convention irregularities. Is there a library that checks how many characters match ? I.e. if 15/31 character match, it's probably the right string. The regex would not be dynamic enough to apply for this sort of thing I think.

I basically want an approximate match, rather than an exact one!

Any help appreciated!

Upvotes: 1

Views: 44

Answers (1)

Terry Spotts
Terry Spotts

Reputation: 4075

difflib should be able to help you with this.

Example:


import difflib

print(difflib.get_close_matches("Man Utd (Supernova) Esports", ["Manchester United (Supernova) Esports", "Liverpool"]))
['Manchester United (Supernova) Esports']

print(difflib.SequenceMatcher(None, "Man Utd (Supernova) Esports", "Manchester United (Supernova) Esports").ratio())
0.84375

Upvotes: 2

Related Questions