“TypeError:string index must be integers not str”从字典键设置变量

2024-10-02 02:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个以字典形式出现的API调用中获取信息。我需要获取一个特定的键并设置一个变量。很容易,但要观察。下面的代码仅适用于所涉及的流程。在

totalEntries = 0  # global var
api_data = requests.get(apiEndpoint).json()

def populateVars():
    global totalEntries
    totalEntries = api_data['total_entries']

api_data的输出仅用于咯咯笑:

^{pr2}$

还有一个错误:

  totalEntries = api_data['total_entries']
TypeError: string indices must be integers, not str

为什么会这样?解决方法是什么?在


Tags: 代码apidataget字典var流程requests

热门问题