python - 在 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)
可以正常工作,但会加载我不需要的数据。
任何帮助,将不胜感激。谢谢
解决方案
尝试使用user.mail_nickname
而不是user['mail_nickname']
. 当对象不支持get
使用方括号(如user['id']
.
或尝试print(dir(user))
。它将帮助您检查可以使用的属性。
推荐阅读
- excel - 循环以将工作表保存在新工作簿中
- scala - 如何在 Scala 中创建不可变的双链表?
- r - ans[npos] <- rep(no, length.out = len)[npos] 中的错误:替换的长度为零
- r - For循环对r中数据框列中的值求和
- javascript - 当 html 在表单中时,我的 JavaScript 不运行
- ios - 为什么 cocoapods 不能在 Apple Silicon 上工作?
- opencv-python - 导入 cv2 错误未定义符号:PyCMethod_New
- ansible - 如何在单个或简短的主机列表上运行剧本并从主机不属于的清单组中获取内容?
- kubernetes - etcd.service 失败,因为超过了超时
- mysql - mysql8.0.22设置默认值DATE_FORMAT(sysdate(),'%Y%m%d')报错