Pichingo
Pichingo

Reputation: 3

Print out a specific key in a python loop

I'm trying to print a certain key from a loop and getting some issues. Tried to search other responses but still having issues.

import json
from azure.graphrbac import GraphRbacManagementClient
from azure.graphrbac.models import UserCreateParameters, PasswordProfile
from azure.common.credentials import UserPassCredentials

admin_user_name = ''
admin_password = ''
# e.g. yourcompany.onmicrosoft.com
tenant_id = ""

credentials = UserPassCredentials(
            admin_user_name,
            admin_password,
            resource="https://graph.windows.net"
    )

graphrbac_client = GraphRbacManagementClient(
    credentials,
    tenant_id
)

users = graphrbac_client.users.list();
for user in users:
    print [user['mail_nickname']]

the key I only want to extract is called mail_nickname

I get this error

TypeError: 'User' object has no attribute '__getitem__'

I only do print(user) that works fine but loads data I don't need.

Any help would be appreciated. Thanks

Upvotes: 0

Views: 55

Answers (1)

sinwoobang
sinwoobang

Reputation: 138

Try to use user.mail_nickname rather than user['mail_nickname']. The error occurs when a object doesn't support get operator using brackets like user['id'].

Or try to print(dir(user)). It would help you to check attributes you can use.

Upvotes: 1

Related Questions