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

Time:03-05

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:

   '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?

CodePudding user response:

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 quite 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.

CodePudding user response:

I think if you use if value.lower() in yourdict[key].lower(): it should work

  • Related