Python 如何正确格式化此API调用?
我正在制作一个电报聊天机器人,不知道如何从输出中取出Python 如何正确格式化此API调用?,python,json,telegram-bot,python-telegram-bot,Python,Json,Telegram Bot,Python Telegram Bot,我正在制作一个电报聊天机器人,不知道如何从输出中取出[{' 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
[{'
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':'f307bdf50d90c92278265cd92819c7870d6652ae3c8af46fa6a96278589b03'}]
这看起来像是一个包含单个dict的列表:
[{'grant': '25000000.00000000',
'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]
您应该能够通过使用[0]
索引列表来访问dict
tetherOut[0]
# {'grant': '25000000.00000000',
# 'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}
…如果您想从dict中获取特定值,可以按其名称编制索引,例如
tetherOut[0]['txid']
# 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'
不过,要小心链接这些东西。如果
tetherOut
是一个空列表,tetherOut[0]
将生成一个索引器
。您可能希望捕获该索引器(以及无效dict键将生成的KeyError
)谢谢!有没有更快的方法格式化{}从中还是我应该定义grant和txid并将其打印在同一行中?@toBePythonNinja,我不明白你的问题。你所说的“格式化{}
的更快方法”是什么意思?我想把它从字典中去掉只需打印:txid:f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03@toBePythonNinja,没有内置的方法可以在dict之外同时打印键和值。但是您可以在字符串中包含一个文本txid:
,例如“上次打印的系带:txid:”
。