[英]How do I stay within a for loop if certain conditions are met, before continuing to the next iteration?
我有三組數據。 我想從 3 個組中收集所有數據。 但是,每個請求我只能獲得 100 條記錄。 每組有100多條記錄。 我只能在我獲得該組的第一批數據后才能知道每個組中有多少數據,這使我看起來無法使用while循環。 這就是我所擁有的。
def getOTCdata(weekStartDate):
#set start and record limit
start = 0
records = 100000
groups = ["G1", "G2", "G3"]
#create for loop to get data with filters
for tier in tier:
params = {
"compareFilters": [
{ "compareType": "equal", "fieldName": "group", "fieldValue": group}
]
}
url = 'myURL'
data = requests.post(url, data=json.dumps(params))
#code to download data - removed so it's not bogged down
#check if the record total is more or less than the data remaining
recordTotal = (data.headers)['Records']
if start + records < recordTotal:
start = +=100
#I WANT TO CONTINUE IN GROUP 1
else:
#MOVE TO GROUP 2
假設 G1 有 150 條記錄。 我希望它再在 G1 中運行一次。 由於我將在第二回合收集所有數據,然后我想移動到 G2。 問題是我在發出請求並下載數據之前不知道記錄總數,所以我不能在 for 循環之后立即使用 while 循環。
使用while循環
recordTotal = (data.headers)['Records']
while start + records < recordTotal:
start += 100
直到 start + records < recordTotal 的條件下才會退出。
如果您希望 start 變量每次增加 100,我還將 start 更改為start += 100
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.