-1

I am trying to match a value of a json file in my python code to another value of another API call within the same code itself. The values basically the same but it does not match because sometimes special characters or trailing/ending space causes a problem

Let's say:

value in the first json file:

json1['org'] = google, LLC    

value in the second json file:

json2['org'] = Google-LLC

Tried using an in operator in the code but it does not work. I am not sure how can I inculcate regex into this one.

So I write an if statement like this:

if json1['org'] in json2['org']:
    # *do something*
else:
    # _do the last thing_

It just keeps on jumping on the else statement even though they are the same.

If the json values are same anyhow regardless of special characters and space, it should match and enter the if statement.

martineau
  • 119,623
  • 25
  • 170
  • 301
Moh
  • 91
  • 2
  • 7
  • 1
    Possible duplicates: [Remove all special characters, punctuation and spaces from string](https://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string) and [How do I do a case-insensitive string comparison?](https://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison) – wjandrea Sep 26 '19 at 22:38
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. StackOverflow is not a design, coding, research, or tutorial resource. – Prune Sep 26 '19 at 22:38
  • assuming that you are trying to match if `google` in `Google-LLC` this is what i'd do. `if json1['org'].split(',').strip()[0].lower() in json2['org'].lower(): *do this* ` I am assuming that your json1 object is separated by , and the text you are trying is the first element. – x85ms16 Sep 26 '19 at 22:39

1 Answers1

1

You could remove all 'special characters/spaces' and compare the values:

import string
asciiAndNumbers = string.ascii_letters + string.digits

json1 = {'org': "google, LLC"}
json2 = {'org': "Google-LLC"}


def normalizedText(text):
    # We are just allowing a-z, A-Z and 0-9 and use lowercase characters
    return ''.join(c for c in text if c in asciiAndNumbers).lower()

j1 = normalizedText(json1['org'])
j2 = normalizedText(json2['org'])

print (j1)
print (j1 == j2)

Prints:

googlellc
True
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47