【问题标题】:Print out a specific key in a python loop在 python 循环中打印出一个特定的键
【发布时间】:2019-01-17 15:18:43
【问题描述】:

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

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) 工作正常但加载我不需要的数据。

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

【问题讨论】:

  • 不就是user的一个属性,例如user.mail_nickname?

标签: python json python-2.7 list loops


【解决方案1】:

尝试使用user.mail_nickname 而不是user['mail_nickname']。 当对象不支持使用 user['id'] 等方括号的 get 运算符时会发生错误。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多