Python—使用While循环,如何更新要在函数中使用的变量字符串

Python—使用While循环,如何更新要在函数中使用的变量字符串,python,debugging,while-loop,twitch,twitch-api,Python,Debugging,While Loop,Twitch,Twitch Api,背景:我正在尝试使用从Twitch的API调用的数据创建一个数据帧。它们每次调用只允许100条记录,因此每次拉动时都会提供一个新的分页光标,以便转到下一页。我使用下面的代码尝试并有效地提取这些数据,而不是手动调整get响应中的after=(分页值)。现在,我试图使动态变量为“Pagination”变量,但它仅在循环完成后更新-没有帮助!看看下面,你是否注意到我可以做些什么来实现这个目标。感谢您的帮助 TwitchTopGamesDataFrame = [] #This is our Data L

背景:我正在尝试使用从Twitch的API调用的数据创建一个数据帧。它们每次调用只允许100条记录,因此每次拉动时都会提供一个新的分页光标,以便转到下一页。我使用下面的代码尝试并有效地提取这些数据,而不是手动调整get响应中的after=(分页值)。现在,我试图使动态变量为“Pagination”变量,但它仅在循环完成后更新-没有帮助!看看下面,你是否注意到我可以做些什么来实现这个目标。感谢您的帮助

TwitchTopGamesDataFrame = [] #This is our Data List                    
BaseURL = 'https://api.twitch.tv/helix/games/top?first=100'
Headers = {'client-id':'lqctse0orgdbs5gdf5faz665api03r','Authorization': 'Bearer a1yl09mwmnwetp6ovocilheias8pzt'}
Indent = 2
Pagination = ''
FullURL = BaseURL + Pagination
Response = requests.get(FullURL,headers=Headers)
iterations = 1 # Data records returned are equivalent to iterations x100

#Loop: Response, Convert JSON data, Append to Data List, Get Pagination & Replace String in Variable - Iterate until 300 records
while count <= 3:
    #Grab JSON Data, Convert, & Append
    ResponseJSONData = Response.json()
    #print(pgn) - Debug
    pd.set_option('display.max_rows', None)
    TopGamesDF = pd.DataFrame(ResponseJSONData['data'])
    TopGamesDF = TopGamesDF[['id','name']]
    TopGamesDF = TopGamesDF.rename(columns={'id':'GameID','name':'GameName'})
    TopGamesDF['Rank'] = TopGamesDF.index + 1
    TwitchTopGamesDataFrame.append(TopGamesDF)
    #print(FullURL) - Debug
    #Grab & Replace Pagination Value
    ResponseJSONData['pagination']
    RPagination = pd.DataFrame(ResponseJSONData['pagination'],index=[0])
    pgn = str('&after='+RPagination.to_string(index=False,header=False).strip())
    Pagination = pgn
    #print(FullURL) - Debug
    iterations += 1
TwitchTopGamesDataFrame```
TwitchTopGamesDataFrame=[]这是我们的数据列表
BaseURL=https://api.twitch.tv/helix/games/top?first=100'
标题={'client-id':'lqctse0orgdbs5gdf5faz665api03r','Authorization':'Bearer a1yl09mwmnwetp6ovocilheias8pzt'}
缩进=2
分页=“”
FullURL=BaseURL+分页
Response=requests.get(FullURL,headers=headers)
迭代次数=1#返回的数据记录相当于迭代次数x100
#循环:响应,转换JSON数据,附加到数据列表,获取分页并替换变量中的字符串-迭代到300条记录
当count发现它时:

def top_games(page_count):
    from time import gmtime, strftime
    strftime("%Y-%m-%d %H:%M:%S", gmtime())
    print("Time of Execution:", strftime("%Y-%m-%d %H:%M:%S", gmtime()))

    #In order to condense the code above and be more efficient, a while/for loop would work great.
    #Goal: Run a While Loop to create a larger DataFrame through Pagination as the Twitch API only allows for 100 records per call.

    baseURL = 'https://api.twitch.tv/helix/games/top?first=100' #Base URL
    Headers = {'client-id':'lqctse0orgdbs5gdf5faz665api03r','Authorization': 'Bearer a1yl09mwmnwetp6ovocilheias8pzt'}
    Indent = 2
    Pagination = ''
    FullURL = BaseURL + Pagination
    Response = requests.get(FullURL,headers=Headers)
    start_count = 0
    count = 0 # Data records returned are equivalent to iterations x100
    max_count = page_count

    #Loop: Response, Convert JSON data, Append to Data List, Get Pagination & Replace String in Variable - Iterate until 300 records
    while count <= max_count:
        #Grab JSON Data, Extend List
        Pagination
        FullURL = baseURL + Pagination
        Response = requests.get(FullURL,headers=Headers)
        ResponseJSONData = Response.json()
        pd.set_option('display.max_rows', None)
        if count == start_count:
            TopGamesDFL = ResponseJSONData['data']
        if count > start_count:
            i = ResponseJSONData['data']
            TopGamesDFL.extend(i)
        #Grab & Replace Pagination Value
        ResponseJSONData['pagination']
        RPagination = pd.DataFrame(ResponseJSONData['pagination'],index=[0])
        pgn = str('&after='+RPagination.to_string(index=False,header=False).strip())
        Pagination = pgn
        count += 1        
        if count == max_count:
            FinalDataFrame = pd.DataFrame(TopGamesDFL)
            FinalDataFrame = FinalDataFrame[['id','name']]
            FinalDataFrame = FinalDataFrame.rename(columns={'id':'GameID','name':'GameName'})
            FinalDataFrame['Rank'] = FinalDataFrame.index + 1
    return FinalDataFrame
def top_游戏(页数):
从时间导入gmtime,strftime
strftime(“%Y-%m-%d%H:%m:%S”,gmtime())
打印(“执行时间:”,strftime(“%Y-%m-%d%H:%m:%S”,gmtime())
#为了压缩上面的代码并提高效率,while/for循环将非常有效。
#目标:运行While循环,通过分页创建更大的数据帧,因为Twitch API只允许每次调用100条记录。
baseURL=https://api.twitch.tv/helix/games/top?first=100“#基本URL
标题={'client-id':'lqctse0orgdbs5gdf5faz665api03r','Authorization':'Bearer a1yl09mwmnwetp6ovocilheias8pzt'}
缩进=2
分页=“”
FullURL=BaseURL+分页
Response=requests.get(FullURL,headers=headers)
开始计数=0
count=0#返回的数据记录相当于迭代次数x100
最大页数=页数
#循环:响应,转换JSON数据,附加到数据列表,获取分页并替换变量中的字符串-迭代到300条记录
计数开始时\u计数:
i=响应JSONDATA['data']
TopGamesDFL.扩展(i)
#获取并替换分页值
ResponseJSONData['pagination']
RPagination=pd.DataFrame(ResponseJSONData['pagination'],index=[0])
pgn=str('&after='+RPagination.to_字符串(index=False,header=False).strip())
分页=pgn
计数+=1
如果计数=最大计数:
FinalDataFrame=pd.DataFrame(TopGamesDFL)
FinalDataFrame=FinalDataFrame[['id','name']]
FinalDataFrame=FinalDataFrame.rename(列={'id':'GameID','name':'GameName'})
FinalDataFrame['Rank']=FinalDataFrame.index+1
返回最终数据帧

计数的初始值?也许零?