删除字典中的双引号



我正在使用一个字典,它的行为在很大程度上像一个字符串,因为它在字典中包含键和值的双引号。我希望能够索引字典,但是演讲标志着阻止我这样做。

我已经搜索并发现了类似的文章,但语音标记是在键上,而不是一个包含字典的大小。

我有:

{'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
final_s = [ast.literal_eval(i) for i in 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']}}}

并允许我在列表内索引:

p[0]

有两个水平('0'0)的嵌套的字典:

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)

:

{'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']}}}

如果您不关心安全性(取决于您如何获取这些字符串)。可以对字符串

使用exec()

命令。例如,如果您想从示例(称为input)中获取信息,您可以执行以下操作

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

现在你可以在real_dict上做任何你想做的事情,就像它是一个普通的字典。

打印出real_dict将会得到:

{'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']}

现在你可以用字典理解对整个输入字典执行此操作。

相关内容

  • 没有找到相关文章

最新更新