acbcccdc
acbcccdc

Reputation: 77

How do I stay within a for loop if certain conditions are met, before continuing to the next iteration?

I have three groups of data. I want to gather all of the data from the 3 groups. However, I can only get 100 records per request. Each group has more than 100 records. I can only tell how much data is in each group after I get the first batch of data for the group, which makes it seem like I can't use a while loop. Here's what I have.

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

Let's assume G1 has 150 records. I want it to run in G1 one more time. Since I will have gathered all the data in the second turn, I'll then want to move to G2. The problem is that I don't know the record total until I make the request and download the data, so I can't use a while loop right after my for loop.

Upvotes: 0

Views: 45

Answers (1)

dlystyr
dlystyr

Reputation: 113

Use a while loop

 recordTotal = (data.headers)['Records']
 while start + records < recordTotal:
     start += 100
        

It will not exit out until the condition of start + records < recordTotal.

I also changed start to start += 100 if you want the start variable to increment by 100 each time.

Upvotes: 1

Related Questions