在使用 2 个不同列表的 json 对象中添加元素

add element in json object working with 2 different lists

我正在尝试将电子邮件对象添加到测试帐户 json。发现自己有点卡在这上面。

理想情况下,我想向 testing_accounts 对象添加一个电子邮件字段。 1 人

json数据

testing_accounts = [{'tenantName': 'hsmd1012159', 'studentName': 'stu1012159', 'studentPassword': 'asdf123!'}, {'tenantName': 'hsmd1012716', 'studentName': 'stu1012716', 'studentPassword': 'asdf123!'}, {'tenantName': 'hsmd1011234', 'studentName': 'stu1011234', 'studentPassword': 'asdf123!'}]

我想添加到上面每个 json 对象的项目。

test = "asdf@gmail.com\nasdf123@gmail.com\ntest@gmail.com"

另一个问题是,如果没有足够的电子邮件来匹配租户,则添加“null@null.com”

期望的输出

desiredOutput = [{
                   'tenantName': 'hsmd1012159', 'studentName': 'stu1012159', 'studentPassword': 'asdf123!', 'email': 'asdf@gmail.com'
                 }, 
                 {
                   'tenantName': 'hsmd1012716', 'studentName': 'stu1012716', 'studentPassword': 'asdf123!', 'email': 'asdf123@gmail.com'
                 },
                 {
                   'tenantName': 'hsmd1011234', 'studentName': 'stu1011234', 'studentPassword': 'asdf123!' , 'email': 'test@gmail.com'
                  },
                 {
                   'tenantName': 'hsmd1011s2234', 'studentName': 'stu122011234', 'studentPassword': 'asdf123!' , 'email': 'null@null'
                 }

]

目前我在哪里....

print(len(testing_accounts))

print(testing_accounts)

def newline(string):
    listRes = list(string.split("\n"))
    return listRes
def comma(string):
    listRes = list(string.split(","))
    return listRes
def comma_and_space(string):
    listRes = list(string.split(", "))
    return listRes


if '\n' in test:
    test1 = newline(test)
    test1 = json.dumps(test1)
    print(test1)
elif ',' in test:
    test1 = comma(test)
    test1 = json.dumps(test1)
    print(test1)
elif ', ' in test:
    test1 = comma_and_space(test)
    test1 = json.dumps(test1)
    print(test1)
else:
    print("didn't hit any of those")
print(len(test1))
print(test1)
for item in testing_accounts:
    for email in test1:
        item.update({'email': email})
    #temp = json.loads(testing_accounts)
    #temp.update({'email': test})
    print(item)

任何指导将不胜感激。

尝试 itertools.zip_longest:

from itertools import zip_longest

test = "asdf@gmail.com\nasdf123@gmail.com\ntest@gmail.com"

for email, account in zip_longest(
    test.split(), testing_accounts, fillvalue="null@null.com"
):
    account["email"] = email

print(testing_accounts)

打印:

[
    {
        "tenantName": "hsmd1012159",
        "studentName": "stu1012159",
        "studentPassword": "asdf123!",
        "email": "asdf@gmail.com",
    },
    {
        "tenantName": "hsmd1012716",
        "studentName": "stu1012716",
        "studentPassword": "asdf123!",
        "email": "asdf123@gmail.com",
    },
    {
        "tenantName": "hsmd1011234",
        "studentName": "stu1011234",
        "studentPassword": "asdf123!",
        "email": "test@gmail.com",
    },
    {
        "tenantName": "hsmd1011s2234",
        "studentName": "stu122011234",
        "studentPassword": "asdf123!",
        "email": "null@null.com",
    },
]