python 如何创建一个函数来比较一个变量的值,如果它不符合模式就返回值?

t9aqgxwy  于 2023-03-11  发布在  Python
关注(0)|答案(1)|浏览(113)

我想创建一个function(),其任务是将sAMAccountName变量的值与排除模式文件中存储的值进行比较。要检查的值是使用for loopencoded_users_from_LDAP.json文件中获取的,模式文件为addc_accounts_excluded.json。在主循环中,我想放置条件if is_account_excluded(sAMAccountName) == False:
我代码的一部分:

with open("addc_accounts_excluded.json", 'r', encoding="UTF-8") as file:
    data = json.load(file)
    excluded_users = data['sAMAccountName']

with open("encoded_users_from_LDAP.json", 'r', encoding="UTF-8") as file:
    data = json.load(file)
    retrived_users = data['entries']

def is_account_excluded(): # this what I need to validate accounts

for user in retrived_users:
    attributes = user['attributes']
    sAMAccountName = attributes['sAMAccountName']
    if is_account_excluded(sAMAccountName) == False:
        print(sAMAccountName)
        print(attributes['cn'])

编辑:我设法解决了这个问题,它工作,但我不知道它是否写正确

def is_account_excluded(suspect):
    account = False
    for account_checked in excluded_users:
        if (account_checked == suspect):
            account = True
    return account
nvbavucw

nvbavucw1#

如果我对你的问题的理解是正确的,这应该工作:

def user_validation(user: str, excluded_users: str):
  return user not in excluded_users

相关问题