从字典中访问一项并将其存储到变量中
Access one item from a dict and store it into a variable
我正在尝试从 API 中获取所有 "uuid",问题是它存储在字典中(我认为)。她是 API:
上的样子
{"guild": {
"_id": "5eba1c5f8ea8c960a61f38ed",
"name": "Creators Club",
"name_lower": "creators club",
"coins": 0,
"coinsEver": 0,
"created": 1589255263630,
"members":
[{ "uuid": "db03ceff87ad4909bababc0e2622aaf8",
"rank": "Guild Master",
"joined": 1589255263630,
"expHistory": {
"2020-06-01": 280,
"2020-05-31": 4701,
"2020-05-30": 0,
"2020-05-29": 518,
"2020-05-28": 1055,
"2020-05-27": 136665,
"2020-05-26": 34806}}]
}
}
现在我对那里的 "uuid" 部分感兴趣,请注意:有多个玩家,可以是 1 到 100 个玩家,我将需要每个 UUID。
现在,我已在 python 中完成此操作,以便在网站上显示 UUID:
尝试:
f = requests.get(
"https://api.hypixel.net/guild?key=[secret]&id=" + guild).json()
guildName = f["guild"]["name"]
guildMembers = f["guild"]["members"]
members = client.getPlayer(uuid=guildMembers) #this converts UUID to player names
#I need to store all uuid's in variables and put them at "guildMembers"
这给了我所有 "UUID codes",我将使用 client.getPlayer(uuid=---)
将 UUID 转换为玩家名称。我必须遍历每个 "UUID" 进入该代码 client.getPlayer(uuid=---)
。但首先我需要将 UUID 保存在变量中,我一直在做 members.uuid
来访问我的 HTML 文件上的 UUID,但我不知道你如何做 .uuid
部分在 python
如果您还需要什么,请评论:)
列表理解是一个强大的概念:
members = [client.getPlayer(member['uuid']) for member in guildMembers]
编辑:
如果您想将名称插入回您的数据中(在 guildMembers 中),
使用 {uuid: member_name,} 格式的字典理解:
members = {member['uuid']: client.getPlayer(uuid=member['uuid']) for member in guildMembers}
你可以用你的结果更新 guildMembers:
for member in guildMembers:
guildMembers[member]['name'] = members[member['uuid']]
假设 guild
是主字典,其中一个名为 members
的键存在于 "sub dictionaries" 的列表中,您可以尝试
uuid = list()
for x in guild['members']:
uuid.append(x['uuid'])
uuid
现在拥有所有 uuid
如果我理解正确的话,你只需要遍历所有接收到的 uuid 并获取玩家的数据。像这样:
f = requests.get("https://api.hypixel.net/guild?key=[secret]&id=" + guild).json()
guildName = f["guild"]["name"]
guildMembers = f["guild"]["members"]
guildMembersData = dict() # Here we will save member's data from getPlayer method
for guildMember in guildMembers:
uuid = guildMember["uuid"]
memberData = client.getPlayer(uuid=uuid)
guildMembersData[uuid] = client.getPlayer(uuid=guildMember["uuid"])
print(guildMembersData) # Here will be players' Data.
我正在尝试从 API 中获取所有 "uuid",问题是它存储在字典中(我认为)。她是 API:
上的样子{"guild": {
"_id": "5eba1c5f8ea8c960a61f38ed",
"name": "Creators Club",
"name_lower": "creators club",
"coins": 0,
"coinsEver": 0,
"created": 1589255263630,
"members":
[{ "uuid": "db03ceff87ad4909bababc0e2622aaf8",
"rank": "Guild Master",
"joined": 1589255263630,
"expHistory": {
"2020-06-01": 280,
"2020-05-31": 4701,
"2020-05-30": 0,
"2020-05-29": 518,
"2020-05-28": 1055,
"2020-05-27": 136665,
"2020-05-26": 34806}}]
}
}
现在我对那里的 "uuid" 部分感兴趣,请注意:有多个玩家,可以是 1 到 100 个玩家,我将需要每个 UUID。
现在,我已在 python 中完成此操作,以便在网站上显示 UUID: 尝试:
f = requests.get(
"https://api.hypixel.net/guild?key=[secret]&id=" + guild).json()
guildName = f["guild"]["name"]
guildMembers = f["guild"]["members"]
members = client.getPlayer(uuid=guildMembers) #this converts UUID to player names
#I need to store all uuid's in variables and put them at "guildMembers"
这给了我所有 "UUID codes",我将使用 client.getPlayer(uuid=---)
将 UUID 转换为玩家名称。我必须遍历每个 "UUID" 进入该代码 client.getPlayer(uuid=---)
。但首先我需要将 UUID 保存在变量中,我一直在做 members.uuid
来访问我的 HTML 文件上的 UUID,但我不知道你如何做 .uuid
部分在 python
如果您还需要什么,请评论:)
列表理解是一个强大的概念:
members = [client.getPlayer(member['uuid']) for member in guildMembers]
编辑: 如果您想将名称插入回您的数据中(在 guildMembers 中), 使用 {uuid: member_name,} 格式的字典理解:
members = {member['uuid']: client.getPlayer(uuid=member['uuid']) for member in guildMembers}
你可以用你的结果更新 guildMembers:
for member in guildMembers:
guildMembers[member]['name'] = members[member['uuid']]
假设 guild
是主字典,其中一个名为 members
的键存在于 "sub dictionaries" 的列表中,您可以尝试
uuid = list()
for x in guild['members']:
uuid.append(x['uuid'])
uuid
现在拥有所有 uuid
如果我理解正确的话,你只需要遍历所有接收到的 uuid 并获取玩家的数据。像这样:
f = requests.get("https://api.hypixel.net/guild?key=[secret]&id=" + guild).json()
guildName = f["guild"]["name"]
guildMembers = f["guild"]["members"]
guildMembersData = dict() # Here we will save member's data from getPlayer method
for guildMember in guildMembers:
uuid = guildMember["uuid"]
memberData = client.getPlayer(uuid=uuid)
guildMembersData[uuid] = client.getPlayer(uuid=guildMember["uuid"])
print(guildMembersData) # Here will be players' Data.