我有包含多个字典的列表形式的JSON数据。我想创建一个新字典,其值与旧字典相同,但键不同。我正在使用原始键并删除下划线以及大写每个单词。问题是,当我创建新字典时,它不会在相同的庄园中创建字典(请参见下面的代码输出)。
如何创建具有与旧词典相同的结构但具有经过修改的键的新词典?
JSON数据
contacts_to_parse = [
{
"contact_order": "1",
"contact_type": "Other",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "2",
"contact_type": "Mail Alias",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "3",
"contact_type": "Mail Alias",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "5",
"contact_type": "Other",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "7",
"contact_type": "Employee",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "8",
"contact_type": "Employee",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "9",
"contact_type": "Other",
"contact_role": "role here",
"contact_text": "contact text here"
}
]
到目前为止的代码:
def check(contacts):
parsed_keys = []
new_dict = {}
for contact in contacts:
for key, val in contact.items():
rm_underscore_capitalize = key.replace('_', ' ').title()
new_dict[rm_underscore_capitalize] = val
if rm_underscore_capitalize in new_dict:
parsed_keys.append(new_dict)
new_dict = dict()
return parsed_keys
if __name__ == '__main__':
con = check(contacts_to_parse)
print(con)
当前输出:
[{'Contact Order': '1'}, {'Contact Type': 'Other'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}, {'Contact Order': '2'}, {'Contact Type': 'Mail Alias'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}, {'Contact Order': '3'}, {'Contact Type': 'Mail Alias'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}, {'Contact Order': '5'}, {'Contact Type': 'Other'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}, {'Contact Order': '7'}, {'Contact Type': 'Employee'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}, {'Contact Order': '8'}, {'Contact Type': 'Employee'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}, {'Contact Order': '9'}, {'Contact Type': 'Other'}, {'Contact Role': 'role here'}, {'Contact Text': 'contact text here'}]
答案 0 :(得分:1)
您需要将内循环结束附近的一些处理拉到外循环。您试图遍历字典列表,然后遍历键以更新'ban-eur.kd_kd_ban-eur<sd,td>'
,但是您却经常重新定义new_dict
。试试这个:
new_dict
答案 1 :(得分:0)
您的逻辑略有偏离。您想将条件移至第二个for循环之外:
def check(contacts):
parsed_keys = []
new_dict = {}
for contact in contacts:
for key, val in contact.items():
rm_underscore_capitalize = key.replace('_', ' ').title()
new_dict[rm_underscore_capitalize] = val
if rm_underscore_capitalize in new_dict:
parsed_keys.append(new_dict)
new_dict = dict()
return parsed_keys
返回:
[
{'Contact Order': '1', 'Contact Type': 'Other', 'Contact Role': 'role here', 'Contact Text': 'contact text here'},
{'Contact Order': '2', 'Contact Type': 'Mail Alias', 'Contact Role': 'role here', 'Contact Text': 'contact text here'},
{'Contact Order': '3', 'Contact Type': 'Mail Alias', 'Contact Role': 'role here', 'Contact Text': 'contact text here'},
{'Contact Order': '5', 'Contact Type': 'Other', 'Contact Role': 'role here', 'Contact Text': 'contact text here'},
{'Contact Order': '7', 'Contact Type': 'Employee', 'Contact Role': 'role here', 'Contact Text': 'contact text here'},
{'Contact Order': '8', 'Contact Type': 'Employee', 'Contact Role': 'role here', 'Contact Text': 'contact text here'},
{'Contact Order': '9', 'Contact Type': 'Other', 'Contact Role': 'role here', 'Contact Text': 'contact text here'}
]
答案 2 :(得分:0)
您期望这样吗?我试图使其变得简单。
contacts_to_parse = [
{
"contact_order": "1",
"contact_type": "Other",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "2",
"contact_type": "Mail Alias",
"contact_role": "role here",
"contact_text": "contact text here"
}
]
def check(contacts):
result = []
for item in contacts:
result.append({})
for key, val in item.items():
result[-1][key.replace('_', ' ').title()] = val
return result
print(check(contacts_to_parse))
结果:
[
{
'Contact Order': '1',
'Contact Type': 'Other',
'Contact Role': 'role here',
'Contact Text': 'contact text here'
},
{
'Contact Order': '2',
'Contact Type': 'Mail Alias',
'Contact Role': 'role here',
'Contact Text': 'contact text here'
}
]
答案 3 :(得分:0)
您可以使用列表推导和字典推导来简化代码:
def check(contacts):
return [_check_contact(contact) for contact in contacts]
def _check_contact(contact):
return {key.replace('_', ' ').title(): value for key, value in contact.items()}
from pprint import pprint
pprint (check(contacts_to_parse))
结果:
[{'Contact Order': '1',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Other'},
{'Contact Order': '2',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Mail Alias'},
{'Contact Order': '3',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Mail Alias'},
{'Contact Order': '5',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Other'},
{'Contact Order': '7',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Employee'},
{'Contact Order': '8',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Employee'},
{'Contact Order': '9',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Other'}]
答案 4 :(得分:-1)
您可以利用列表和字典理解:
contacts_to_parse = [
{
"contact_order": "1",
"contact_type": "Other",
"contact_role": "role here",
"contact_text": "contact text here"
},
{
"contact_order": "2",
"contact_type": "Mail Alias",
"contact_role": "role here",
"contact_text": "contact text here"
},
]
from pprint import pprint
pprint([{k.replace('_', ' ').title():v for k, v in contact.items()} for contact in contacts_to_parse])
打印:
[{'Contact Order': '1',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Other'},
{'Contact Order': '2',
'Contact Role': 'role here',
'Contact Text': 'contact text here',
'Contact Type': 'Mail Alias'}]