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]
CodePudding user response:
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']}}}
CodePudding user response:
import ast
D = {'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']}"}}
D['0'][0] = ast.literal_eval(D['0'][0])
print(D)