我有这段代码从字典对象中提取值
extracted_value = response_content["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["lastPayment"]["status"]
extracted_value = response_content["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["bill"]["dueDate"]
这些只是两个样本,但我有十几个具有不同的键/路径组合。 我怎样才能使用模块调用它们并执行类似的操作
def get_value_from_content (response_content, my_path):
# how can I use the value in my_path as the key instead of this hard coded path ?
extracted_value = response_content["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["lastPayment"]["status"]
#extracted_value = response_content using my_path is what I would like to do
return extracted_value
#I get this from a REST API call but skipping the code here and just hard coding to ask the question here
response_content = {u'retrievePolicyBillingSummariesResponse': {u'billingSummaries': {u'policyBillingSummary': [{u'policy': {u'status': u'A', u'policyNumber': u'xyz123', u'writingCompany': u'FBI', u'renewalFlag': u'false', u'convertedRenewalOffer': u'false', u'termExpirationDate': u'2017-06-26', u'lineOfBusiness': u'PC', u'termEffectiveDate': u'2016-06-26', u'riskState': u'CA', u'insureds': {u'namedInsuredSummary': [{u'preferredPostalAddress': {u'streetAddressLine': u'1 disney', u'cityName': u'palo alto', u'zipCode': u'94100', u'isoRegionCode': u'CA'}, u'name': {u'lastName': u'DOE', u'fullName': u'john doe', u'firstName': u'john'}}]}, u'additionalInterests': {u'additionalInterest': [{u'billTo': u'N', u'name': {u'partyType': u'Organization'}}]}, u'type': u'PA', u'statusDescription': u'Active', u'dataSource': u'from_heaven'}, u'billingSummary': {u'paymentRestriction': u'false', u'nextInstallmentAmount': u'0.00', u'bill': {u'installmentNumber': u'1', u'statementDate': u'2016-06-26', u'paymentPlan': u'Direct', u'installmentAmount': u'12.00', u'totalBillAmountDue': u'1.76', u'previousBalance': u'0.00', u'dueDate': u'2016-06-26', u'billingPlan': u'ANN'}, u'lastPayment': {u'status': u'A'}, u'currentBalance': u'16.66', u'payOffAmount': u'15.66', u'isRestrictedToPay': u'false'}}]}}}
my_path = '["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["lastPayment"]["status"]'
get_extracted_item = get_value_from_content(response_content,my_path)
my_path = '["retrievePolicyBillingSummariesResponse"]["billingSummaries"]["policyBillingSummary"][0]["billingSummary"]["bill"]["dueDate"]'
get_extracted_item = get_value_from_content(response_content,my_path)
答案 0 :(得分:2)
首先,编写像
这样的小实用功能会更容易def extract_from_dictionary(dictionary, *keys_or_indexes):
value = dictionary
for key_or_index in keys_or_indexes:
value = value[key_or_index]
return value
我们可以从您的示例中看到,有一个名为billingSummary
的对象出现在必需的路径中,因此我们可以避免使用
def get_billing_summary(response_content):
return extract_from_dictionary(
response_content,
"retrievePolicyBillingSummariesResponse",
"billingSummaries",
"policyBillingSummary",
0,
"billingSummary")
然后我们可以简单地写
def get_value_from_content(response_content, *keys):
billing_summary = get_billing_summary(response_content)
extracted_value = extract_from_dictionary(billing_summary,
*keys)
return extracted_value
获取所需的对象,如
last_payment_status = get_value_from_content(response_content,
"lastPayment",
"status")
bill_due_date = get_value_from_content(response_content,
"bill",
"dueDate")
print("last_payment_status:", last_payment_status)
print("bill_due_date:", bill_due_date)
给我们
last_payment_status: A
bill_due_date: 2016-06-26
答案 1 :(得分:0)
如何使用迭代解决方案,如下所示:
response_content = {u'retrievePolicyBillingSummariesResponse': {u'billingSummaries': {u'policyBillingSummary': [{u'policy': {u'status': u'A', u'policyNumber': u'xyz123', u'writingCompany': u'FBI', u'renewalFlag': u'false', u'convertedRenewalOffer': u'false', u'termExpirationDate': u'2017-06-26', u'lineOfBusiness': u'PC', u'termEffectiveDate': u'2016-06-26', u'riskState': u'CA', u'insureds': {u'namedInsuredSummary': [{u'preferredPostalAddress': {u'streetAddressLine': u'1 disney', u'cityName': u'palo alto', u'zipCode': u'94100', u'isoRegionCode': u'CA'}, u'name': {u'lastName': u'DOE', u'fullName': u'john doe', u'firstName': u'john'}}]}, u'additionalInterests': {u'additionalInterest': [{u'billTo': u'N', u'name': {u'partyType': u'Organization'}}]}, u'type': u'PA', u'statusDescription': u'Active', u'dataSource': u'from_heaven'}, u'billingSummary': {u'paymentRestriction': u'false', u'nextInstallmentAmount': u'0.00', u'bill': {u'installmentNumber': u'1', u'statementDate': u'2016-06-26', u'paymentPlan': u'Direct', u'installmentAmount': u'12.00', u'totalBillAmountDue': u'1.76', u'previousBalance': u'0.00', u'dueDate': u'2016-06-26', u'billingPlan': u'ANN'}, u'lastPayment': {u'status': u'A'}, u'currentBalance': u'16.66', u'payOffAmount': u'15.66', u'isRestrictedToPay': u'false'}}]}}}
my_path = [
"retrievePolicyBillingSummariesResponse",
"billingSummaries",
"policyBillingSummary",
0,
"billingSummary",
"lastPayment",
"status"
]
def get_value_from_content(extraction, my_path):
for el in my_path:
if isinstance(extraction, dict):
extraction = extraction.get(el, extraction)
else:
extraction = extraction[el]
return extraction
extraction = get_value_from_content(response_content, my_path)
print(extraction)
函数get_value_from_content
甚至可以比之前更短,即
def get_value_from_content(extraction, my_path):
for el in my_path:
extraction = extraction[el]
return extraction
get_value_from_content
的最后一个版本更容易抛出例外情况,例如:误读了路径组件的链接。因此,仍然需要确定字符串对象my_path
是人为还是机器制造的。
在这两种情况下,返回"A"
。 Python 2和3中的 经过测试 。另请注意,自the former is usually faster than the latter以来,我赞成对递归的迭代解决方案。在20% and 40% faster in the present case之间。
话虽如此,这并未解决问题,因为my_path
最初不是列表对象,而是字符串对象。 要解决问题,首先要将此字符串转换为键/索引列表,然后按上述方法处理它。或者,正如@Minji所做的那样,人们可能想要使用python内置函数eval
。即使使用此函数被称为a bad practice,我也想知道在这种情况下,eval
的使用在多大程度上无法描述为最佳方式。
答案 2 :(得分:-1)
递归函数怎么样?
def get_value(response, index):
if len(index) > 1:
return get_value(response[index[0]], index[1:])
else:
return response[index[0]]
index = ["retrievePolicyBillingSummariesResponse", "billingSummaries", "policyBillingSummary", 0, "billingSummary", "lastPayment", "status"]
get_value(response_content, index)
答案 3 :(得分:-1)
eval()
将字符串解释为代码
def get_value_from_content (response_content, my_path):
# string is arguments name
item = "response_content" + my_path
return eval(item)