我正在制作一个电报聊天机器人,但不知道如何从输出中取出 [{'
。
def tether(bot, update):
tetherCall = "https://api.omniexplorer.info/v1/property/31"
tetherCallJson = requests.get(tetherCall).json()
tetherOut = tetherCallJson ['issuances'][:1]
update.message.reply_text("Last printed tether: " + str (tetherOut)+" Please take TXID and past it in this block explorer to see more info: https://www.omniexplorer.info/search")
我的用户将看到此响应:[{'grant': '25000000.00000000', 'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]
最佳答案
这看起来像一个包含单个字典的列表:
[{'grant': '25000000.00000000',
'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]
您应该能够通过使用 [0]
对列表进行索引来访问字典...
tetherOut[0]
# {'grant': '25000000.00000000',
# 'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}
...如果您想从字典中获取特定值,您可以按其名称进行索引,例如
tetherOut[0]['txid']
# 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'
但是要小心链接这些东西。如果 tetherOut
是空列表,tetherOut[0]
将生成 IndexError
。您可能想要捕获它(以及无效字典键将生成的 KeyError
)。
关于python - 如何正确设置此 API 调用的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020056/