我从一个以字典形式出现的API调用中获取信息。我需要获取一个特定的键并设置一个变量。很容易,但要观察。下面的代码仅适用于所涉及的流程。在
totalEntries = 0 # global var
api_data = requests.get(apiEndpoint).json()
def populateVars():
global totalEntries
totalEntries = api_data['total_entries']
api_data
的输出仅用于咯咯笑:
还有一个错误:
totalEntries = api_data['total_entries']
TypeError: string indices must be integers, not str
为什么会这样?解决方法是什么?在
正如jonsharpe已经指出的,您的
api_data
是一个字符串,它只能用整数索引。在而不是使用:
考虑使用^{} :
^{pr2}$相关问题 更多 >
编程相关推荐