[英]How to insert list values to a dictionary in Python?
如何在字典中的與會者列表中插入多個 emailAddress 字段? 我有兩個列表地址,名字。 我想遍歷這些列表來生成這個字典。 FE
addresses = ["test@test.test", "nana@nana.nana"]
names = ["test", "nana"]
結果應該是這樣的:
{
"subject": "Let's go for lunch",
"body": {
"contentType": "HTML",
"content": "Does mid month work for you?"
},
"start": {
"dateTime": "2020-02-10T12:00:00",
"timeZone": "Pacific Standard Time"
},
"end": {
"dateTime": "2020-02-10T14:00:00",
"timeZone": "Pacific Standard Time"
},
"location": {
"displayName": "Harry's Bar"
},
"attendees": [
{
"emailAddress": {
"address": "test@test.test",
"name": "test"
},
"type": "required"
},
{
"emailAddress": {
"address": "nana@nana.nana",
"name": "nana"
},
"type": "required"
}
],
"isOnlineMeeting": true,
"onlineMeetingProvider": "teamsForBusiness"
}
我試圖遍歷每個列表並創建一個字符串:
for a in addresses:
for n in names:
attendees_body +='{"emailAddress": {"address": "%s", "name": "%s"}, "type": "required"},' % (a,n)
然后我嘗試將此字符串添加到字典中,並使用 json.dumps(dictionary) 格式化 json 文件(我需要)。 但后來我得到了看起來像這樣的結果
{"subject": "subjectas", "start": {"dateTime": "2020-02-10T12:00:00", "timeZone": "Pacific Standard Time"}, "end": {"dateTime": "2020-02-10T14:00:00", "timeZone": "Pacific Standard Time"}, "location": {"displayName": "Harry's Bar"}, "attendees": ["{'emailAddress': {'address': 'sasi@sasi.xui', 'name': 'zdarovenko'}, 'type': 'required'},{'emailAddress': {'address': 'sasi@sasi.xui', 'name': 'kurwenko'}, 'type': 'required'},{'emailAddress': {'address': 'nana@sasi.xui', 'name': 'zdarovenko'}, 'type': 'required'},{'emailAddress': {'address': 'nana@sasi.xui', 'name': 'kurwenko'}, 'type': 'required'},"], "isOnlineMeeting": true, "onlineMeetingProvider": "teamsForBusiness"}
有些值在 "" 中,有些在 ''..
目前還不清楚你在問什么,但本質上我認為這是一個兩部分的問題:
假設這是正確的,那么您可以簡單地使用字典列表中的 append 方法。 您需要用於從兩個列表中獲取聚合數據的 function 使用zip解析,該 zip 采用可迭代對象(在本例中為列表),然后將它們聚合到一個元組中。
我應該說這里不需要 json,我只是用它而不是 pprint 來獲得很好的格式。
import json
from collections import defaultdict
addresses = ["test@test.test", "nana@nana.nana"]
names = ["test", "nana"]
my_dictionary = {
"subject": "Let's go for lunch",
"body": {
"contentType": "HTML",
"content": "Does mid month work for you?"
},
"start": {
"dateTime": "2020-02-10T12:00:00",
"timeZone": "Pacific Standard Time"
},
"end": {
"dateTime": "2020-02-10T14:00:00",
"timeZone": "Pacific Standard Time"
},
"location": {
"displayName": "Harry's Bar"
},
"attendees": [
],
"isOnlineMeeting": True,
"onlineMeetingProvider": "teamsForBusiness"
}
for a, n in zip(addresses, names):
contact = {
"emailAddress":
{
"address": a,
"name": n
},
"type": "required"
}
my_dictionary['attendees'].append(contact)
print(json.dumps(my_dictionary, indent=4, default=str))
這將產生以下結果:
{
"subject": "Let's go for lunch",
"body": {
"contentType": "HTML",
"content": "Does mid month work for you?"
},
"start": {
"dateTime": "2020-02-10T12:00:00",
"timeZone": "Pacific Standard Time"
},
"end": {
"dateTime": "2020-02-10T14:00:00",
"timeZone": "Pacific Standard Time"
},
"location": {
"displayName": "Harry's Bar"
},
"attendees": [
{
"emailAddress": {
"address": "test@test.test",
"name": "test"
},
"type": "required"
},
{
"emailAddress": {
"address": "nana@nana.nana",
"name": "nana"
},
"type": "required"
}
],
"isOnlineMeeting": true,
"onlineMeetingProvider": "teamsForBusiness"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.