Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I do a wildcard search for a dict of jsons?

Given this data:

gics_json = """
     {'Trading Companies & Distributors': [{'': '460',
       'CIK': '1067701',
       'Date first added': '2014-09-20',
       'Founded': '1997',
       'GICS Sector': 'Industrials',
       'GICS Sub-Industry': 'Trading Companies & Distributors',
       'Headquarters Location': 'Stamford, Connecticut',
       'SEC filings': 'reports',
       'Security': 'United Rentals, Inc.',
       'Symbol': 'URI'}],
     'Trucking': [{'': '262',
       'CIK': '728535',
       'Date first added': '2015-07-01',
       'Founded': '1961',
       'GICS Sector': 'Industrials',
       'GICS Sub-Industry': 'Trucking',
       'Headquarters Location': 'Lowell, Arkansas',
       'SEC filings': 'reports',
       'Security': 'J. B. Hunt Transport Services',
       'Symbol': 'JBHT'},
      {'': '351',
       'CIK': '878927',
       'Date first added': '2019-12-09',
       'Founded': '1934',
       'GICS Sector': 'Industrials',
       'GICS Sub-Industry': 'Trucking',
       'Headquarters Location': 'Thomasville, North Carolina',
       'SEC filings," "reports',
       'Security': 'Old Dominion Freight Line',
       'Symbol': 'ODFL'}]} """

gics_dict = json.loads(gics_json)

I want to find the GICS Sector/Sub-Industry section that’s related to a search term.

For example, if I search for Dominion, I want to see:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   'GICS Sector': 'Industrials',
   'GICS Sub-Industry': 'Trucking'

I have been struggling, here’s my (not working code):

    def searcher(value, yourdict):
      for key in yourdict:
          if value.lower() in yourdict[key]:
              print(yourdict[key][value])
searcher('dominion', gics_dict)

But I get no results. Any suggestions of what I’m doing wrong?

>Solution :

I had to make slight syntactic changes to gics_dict, but assuming this is what you get from json.loads(gics_json), then there are wuite a few modifications needed to searcher() to drill down into the structure:

gics_dict = {'Trading Companies & Distributors': [{'': '460',
       'CIK': '1067701',
       'Date first added': '2014-09-20',
       'Founded': '1997',
       'GICS Sector': 'Industrials',
       'GICS Sub-Industry': 'Trading Companies & Distributors',
       'Headquarters Location': 'Stamford, Connecticut',
       'SEC filings': 'reports',
       'Security': 'United Rentals, Inc.',
       'Symbol': 'URI'}],
     'Trucking': [{'': '262',
       'CIK': '728535',
       'Date first added': '2015-07-01',
       'Founded': '1961',
       'GICS Sector': 'Industrials',
       'GICS Sub-Industry': 'Trucking',
       'Headquarters Location': 'Lowell, Arkansas',
       'SEC filings': 'reports',
       'Security': 'J. B. Hunt Transport Services',
       'Symbol': 'JBHT'},
      {'': '351',
       'CIK': '878927',
       'Date first added': '2019-12-09',
       'Founded': '1934',
       'GICS Sector': 'Industrials',
       'GICS Sub-Industry': 'Trucking',
       'Headquarters Location': 'Thomasville, North Carolina',
       'SEC filings': 'reports',
       'Security': 'Old Dominion Freight Line',
       'Symbol': 'ODFL'}]}


def searcher(value, yourdict):
    vl = value.lower()
    found = []
    for area in yourdict:
        for cik in yourdict[area]:
          for key in cik:
              if vl in cik[key].lower():
                  found.append(cik)
    for cik in found:
        print(cik['CIK'])
        print(f"'GICS Sector': {cik['GICS Sector']}")
        print(f"'GICS Sub-Industry': {cik['GICS Sub-Industry']}")
    
    #return found

searcher('dominion', gics_dict)

Output:

878927
'GICS Sector': Industrials
'GICS Sub-Industry': Trucking

Note that I suggest, from a design point of view, that you remove the for loop with the printing and add in return found and make another function do any required printing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading