Stackcans
Stackcans

Reputation: 351

Remove double quotes from dictionary

I'm working with a dictionary which seems to behave like a string for the most part because it has double quotes across the dictionary encompassing the keys and values. I want to be able to index the dictionary, however the speech marks prevent me from doing so.

I have searched and found similar articles but the speech marks were on the keys as opposed to a size encompassing a dictionary.

Here's what I have:

{'0': {0: "{'address_components': [{'long_name': '238', 'short_name': '238', "
      "'types': ['street_number']}, {'long_name': 'Lincoln Street', "
      "'short_name': 'Lincoln St', 'types': ['route']}, {'long_name': "
      "'Hahnville', 'short_name': 'Hahnville', 'types': ['locality', "
      "'political']}, {'long_name': 'St. Charles Parish', 'short_name': "
      "'St Charles Parish', 'types': ['administrative_area_level_2', "
      "'political']}, {'long_name': 'Louisiana', 'short_name': 'LA', "
      "'types': ['administrative_area_level_1', 'political']}, "
      "{'long_name': 'United States', 'short_name': 'US', 'types': "
      "['country', 'political']}, {'long_name': '70057', 'short_name': "
      "'70057', 'types': ['postal_code']}], 'formatted_address': '238 "
      "Lincoln St, Hahnville, LA 70057, USA', 'geometry': {'bounds': "
      "{'northeast': {'lat': 29.9765067, 'lng': -90.4105124}, 'southwest': "
      "{'lat': 29.9763491, 'lng': -90.4107531}}, 'location': {'lat': "
      "29.97642, 'lng': -90.4106589}, 'location_type': 'ROOFTOP', "
      "'viewport': {'northeast': {'lat': 29.9777768802915, 'lng': "
      "-90.4092837697085}, 'southwest': {'lat': 29.9750789197085, 'lng': "
      "-90.4119817302915}}}, 'place_id': 'ChIJu32jB3_PIIYRMF2Utx14Ouc', "
      "'types': ['premise']}"}}

And what I have tried:

import ast
final_s = [ast.literal_eval(i) for i in p]

Which returns the wrong expected results.

The expected result should remove the double speech marks:

{'0': {0: {'address_components': [{'long_name': '238', 'short_name': '238', 'types':
   ['street_number']}, {'long_name': 'Lincoln Street', 'short_name': 'Lincoln St',
   'types': ['route']}, {'long_name': 'Hahnville', 'short_name': 'Hahnville', 'types':
   ['locality', 'political']}, {'long_name': 'St. Charles Parish', 'short_name': 'St.
  Charles Parish', 'types': ['administrative_area_level_2', 'political']},
 {'long_name':   'Louisiana', 'short_name': 'LA', 'types':
 ['administrative_area_level_1',   'political']}, {'long_name': 'United States',
 'short_name': 'US', 'types': ['country',   'political']}, {'long_name': '70057',
 'short_name': '70057', 'types':   ['postal_code']}], 'formatted_address': '238 Lincoln
 St, Hahnville, LA 70057, USA',   'geometry': {'bounds': {'northeast': {'lat':
 29.9765067, 'lng': -90.4105124},   'southwest': {'lat': 29.9763491, 'lng':
 -90.4107531}}, 'location': {'lat': 29.97642,   'lng': -90.4106589}, 'location_type':
 'ROOFTOP', 'viewport': {'northeast': {'lat':   29.9777768802915, 'lng':
 -90.4092837697085}, 'southwest': {'lat': 29.9750789197085,   'lng':
 -90.4119817302915}}}, 'place_id': 'ChIJu32jB3_PIIYRMF2Utx14Ouc', 'types':
   ['premise']}}}

And allow for me to index within the list:

p[0]

Upvotes: 2

Views: 134

Answers (2)

Warkaz
Warkaz

Reputation: 903

If you aren't concerned about security (depends how you acquire those strings). You can use the exec() command on the string

For example if you want to get info from your example (dubbed as input you can do the following)

exec("real_dict="+input['0'][0])

and now you can do anything you want on the real_dict like it was a normal dictionary.

Printing out real_dict would result you in:

{'address_components': [{'long_name': '238',
   'short_name': '238',
   'types': ['street_number']},
  {'long_name': 'Lincoln Street',
   'short_name': 'Lincoln St',
   'types': ['route']},
  {'long_name': 'Hahnville',
   'short_name': 'Hahnville',
   'types': ['locality', 'political']},
  {'long_name': 'St. Charles Parish',
   'short_name': 'St Charles Parish',
   'types': ['administrative_area_level_2', 'political']},
  {'long_name': 'Louisiana',
   'short_name': 'LA',
   'types': ['administrative_area_level_1', 'political']},
  {'long_name': 'United States',
   'short_name': 'US',
   'types': ['country', 'political']},
  {'long_name': '70057', 'short_name': '70057', 'types': ['postal_code']}],
 'formatted_address': '238 Lincoln St, Hahnville, LA 70057, USA',
 'geometry': {'bounds': {'northeast': {'lat': 29.9765067, 'lng': -90.4105124},
   'southwest': {'lat': 29.9763491, 'lng': -90.4107531}},
  'location': {'lat': 29.97642, 'lng': -90.4106589},
  'location_type': 'ROOFTOP',
  'viewport': {'northeast': {'lat': 29.9777768802915,
    'lng': -90.4092837697085},
   'southwest': {'lat': 29.9750789197085, 'lng': -90.4119817302915}}},
 'place_id': 'ChIJu32jB3_PIIYRMF2Utx14Ouc',
 'types': ['premise']}

Now you can do this to the whole input dictionary with dictionary comprehension.

Upvotes: 0

Maurice Meyer
Maurice Meyer

Reputation: 18106

There are 2 levels ('0' and 0) in the nested dictionary:

p = {
    '0':
        {
            0:
                "{'address_components': [{'long_name': '238', 'short_name': '238', 'types': ['street_number']}, {'long_name': 'Lincoln Street', 'short_name': 'Lincoln St', 'types': ['route']}, {'long_name': 'Hahnville', 'short_name': 'Hahnville', 'types': ['locality', 'political']}, {'long_name': 'St. Charles Parish', 'short_name': 'St Charles Parish', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Louisiana', 'short_name': 'LA', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '70057', 'short_name': '70057', 'types': ['postal_code']}], 'formatted_address': '238 Lincoln St, Hahnville, LA 70057, USA', 'geometry': {'bounds': {'northeast': {'lat': 29.9765067, 'lng': -90.4105124}, 'southwest': {'lat': 29.9763491, 'lng': -90.4107531}}, 'location': {'lat': 29.97642, 'lng': -90.4106589}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 29.9777768802915, 'lng': -90.4092837697085}, 'southwest': {'lat': 29.9750789197085, 'lng': -90.4119817302915}}}, 'place_id': 'ChIJu32jB3_PIIYRMF2Utx14Ouc', 'types': ['premise']}"
        }
}

import ast, pprint

for k, value in p.items():
    for sk, subValue in value.items():
        p[k][sk] = ast.literal_eval(subValue)

pprint.pprint(p)

Out:

{'0': {0: {'address_components': [{'long_name': '238',
                                   'short_name': '238',
                                   'types': ['street_number']},
                                  {'long_name': 'Lincoln Street',
                                   'short_name': 'Lincoln St',
                                   'types': ['route']},
                                  {'long_name': 'Hahnville',
                                   'short_name': 'Hahnville',
                                   'types': ['locality', 'political']},
                                  {'long_name': 'St. Charles Parish',
                                   'short_name': 'St Charles Parish',
                                   'types': ['administrative_area_level_2',
                                             'political']},
                                  {'long_name': 'Louisiana',
                                   'short_name': 'LA',
                                   'types': ['administrative_area_level_1',
                                             'political']},
                                  {'long_name': 'United States',
                                   'short_name': 'US',
                                   'types': ['country', 'political']},
                                  {'long_name': '70057',
                                   'short_name': '70057',
                                   'types': ['postal_code']}],
           'formatted_address': '238 Lincoln St, Hahnville, LA 70057, USA',
           'geometry': {'bounds': {'northeast': {'lat': 29.9765067,
                                                 'lng': -90.4105124},
                                   'southwest': {'lat': 29.9763491,
                                                 'lng': -90.4107531}},
                        'location': {'lat': 29.97642, 'lng': -90.4106589},
                        'location_type': 'ROOFTOP',
                        'viewport': {'northeast': {'lat': 29.9777768802915,
                                                   'lng': -90.4092837697085},
                                     'southwest': {'lat': 29.9750789197085,
                                                   'lng': -90.4119817302915}}},
           'place_id': 'ChIJu32jB3_PIIYRMF2Utx14Ouc',
           'types': ['premise']}}}

Upvotes: 3

Related Questions