首页 > 解决方案 > 在 python 循环中打印出一个特定的键

问题描述

我正在尝试从循环中打印某个键并遇到一些问题。尝试搜索其他回复,但仍有问题。

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']]

我只想提取的密钥被称为mail_nickname

我收到这个错误

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

我只这样做print(user)可以正常工作,但会加载我不需要的数据。

任何帮助,将不胜感激。谢谢

标签: pythonjsonpython-2.7listloops

解决方案


尝试使用user.mail_nickname而不是user['mail_nickname']. 当对象不支持get使用方括号(如user['id'].

或尝试print(dir(user))。它将帮助您检查可以使用的属性。


推荐阅读