【发布时间】:2020-09-26 17:03:56
【问题描述】:
我有一个这样的字典:
{
'StreetAddress': {
'type': 'string',
'valueString': '4| 7 RULE CHIE N',
'text': '4| 7 RULE CHIE N',
'page': 1,
'boundingBox': [
76.0,
342.0,
829.0,
342.0,
829.0,
382.0,
76.0,
382.0
],
'confidence': 1.0
},
'Phone': {
'type': 'string',
'valueString': '5 4 3 | 9 7 | 0 0 1',
'text': '5 4 3 | 9 7 | 0 0 1',
'page': 1,
'boundingBox': [
77.0,
465.0,
648.0,
465.0,
648.0,
502.0,
77.0,
502.0
],
'confidence': 1.0
},
'FirstName': {
'type': 'string',
'valueString': 'HENRI',
'text': 'HENRI',
'page': 1,
'boundingBox': [
73.0,
291.0,
341.0,
291.0,
341.0,
322.0,
73.0,
322.0
],
'confidence': 1.0
},
'LastName': {
'type': 'string',
'valueString': 'AC THANICE',
'text': 'AC THANICE',
'page': 1,
'boundingBox': [
138.0,
224.0,
521.0,
224.0,
521.0,
258.0,
138.0,
258.0
],
'confidence': 0.986
},
'City': {
'type': 'string',
'valueString': 'MIO N REAL',
'text': 'MIO N REAL',
'page': 1,
'boundingBox': [
67.0,
398.0,
527.0,
398.0,
527.0,
451.0,
67.0,
451.0
],
'confidence': 0.997
},
'PostalCode': {
'type': 'string',
'valueString': 'H 3 B O A 2',
'text': 'H 3 B O A 2',
'page': 1,
'boundingBox': [
927.0,
411.0,
1249.0,
411.0,
1249.0,
444.0,
927.0,
444.0
],
'confidence': 1.0
},
'Province': {
'type': 'string',
'valueString': 'Q C',
'text': 'Q C',
'page': 1,
'boundingBox': [
792.0,
410.0,
842.0,
410.0,
842.0,
436.0,
792.0,
436.0
],
'confidence': 1.0
},
'FormId': {
'type': 'string',
'valueString': 'C1234567',
'text': 'C1234567',
'page': 1,
'boundingBox': [
972.0,
712.0,
1268.0,
712.0,
1268.0,
766.0,
972.0,
766.0
],
'confidence': 0.96
},
'DonationDate': {
'type': 'string',
'valueString': '2 6 0 6',
'text': '2 6 0 6',
'page': 1,
'boundingBox': [
72.0,
165.0,
395.0,
165.0,
395.0,
198.0,
72.0,
198.0
],
'confidence': 1.0
},
'Email': {
'type': 'string',
'valueString': 'h- lach @ gmail.com',
'text': 'h- lach @ gmail.com',
'page': 1,
'boundingBox': [
252.0,
516.0,
685.0,
516.0,
685.0,
559.0,
252.0,
559.0
],
'confidence': 0.57
},
'DonationAmount': {
'type': 'string',
'valueString': '600.00',
'text': '600.00',
'page': 1,
'boundingBox': [
623.0,
162.0,
776.0,
162.0,
776.0,
191.0,
623.0,
191.0
],
'confidence': 1.0
},
'UnitAddress': None
}
我需要像这样创建一个字典:
resultsData = {'StreetAddress': '4| 7 RULE CHIE N',
'Phone': '5 4 3 | 9 7 | 0 0 1',
'FirstName': 'HENRI',
'LastName': 'AC THANICE',
'City': 'MIO N REAL',
'PostalCode': 'H 3 B O A 2',
'Province': 'Q C',
'FormId': 'C1234567',
'DonationDate': '2 6 0 6',
'Email': 'h- lach @ gmail.com',
'DonationAmount': '600.00'}
resultsData字典中的item结合了fieldData的outer_keys (即:'StreetAddress'、'Phone'、'FirstName'...)和 inner_key 的 inner_value,如果 inner_key == 'text' (即:outer_key == 'FirstName,inner_key == 'text',inner_value == 'HENRI')
我已经尝试过来自 datacamp dictionary 的基于循环的指导
resultsData = {}
for (outer_k, outer_v) in fieldData.items():
for (inner_k, inner_v) in outer_v.items():
if inner_k == 'text':
resultsData.update({outer_k:inner_v})
这会创建一个我正在寻找的对象(如上所述)但会引发错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-27-dbae6918239f> in <module>
1 resultsData = {}
2 for (outer_k, outer_v) in fieldData.items():
----> 3 for (inner_k, inner_v) in outer_v.items():
4 if inner_k == 'text':
5 resultsData.update({outer_k:inner_v})
AttributeError: 'NoneType' object has no attribute 'items'
我也尝试过基于 Nested dictionary comprehension python 模式的字典理解
data = {outer_key: {inner_value: inner_key for inner_value, inner_key in outer_value.items()}
for outer_key, inner_value in fieldData.items()}
返回错误:
NameError Traceback (most recent call last)
<ipython-input-114-c09167c38348> in <module>
1 data = {outer_key: {inner_value: inner_key for inner_value, inner_key in outer_value.items()}
----> 2 for outer_key, inner_value in fieldData.items()}
<ipython-input-114-c09167c38348> in <dictcomp>(.0)
1 data = {outer_key: {inner_value: inner_key for inner_value, inner_key in outer_value.items()}
----> 2 for outer_key, inner_value in fieldData.items()}
NameError: name 'outer_value' is not defined
非常感谢任何解决问题的建议。
【问题讨论】:
-
您能分享完整的
fieldData吗?似乎有一个字典条目没有太多价值 -
添加了完整的字典
-
谢谢。请检查我的答案。
-
谢谢大家的回答。他们指示我添加一个函数来清理无字典 - 没有它,编辑字段内容的 Web 应用程序会出现空引用错误。
标签: python dictionary-comprehension