Home > front end >  Iterate and loop through certain nested values to use within JSON Data and Request URLs
Iterate and loop through certain nested values to use within JSON Data and Request URLs

Time:07-04

i_ucprice = [[id, price], [id, price], [id, price], [id, price]]

    for ids in id_ucprice:
        post_data = {
            'price': ids[1],
            'overpriced_email_price_change': False,
            'apply_to_all': False,
            }
        for i in range(ids[0]):
            response = requests.post('api{ids[0]}/pricing', cookies=cookies, headers=headers, json=post_data)
            update_data = json.loads(response.text)
            print(update_data)
            response_price = update_data['priceCents']
            r_item = update_data['name']
            r_id = update_data['id']
            ids[0]

        if response_price == ids[1]:
            print(style.MAGENTA   f'[{datetime.now()}] => Updated Price: {response_price/100:.2f} | ID: {r_id} | Item: {r_item}')

I have an nested list that works as [[id, price], [id, price], [id, price], [id, price]] and I need to iterate through the [0] and the [1] value to use in the request url and json data respectively. I can't get the variable to be used in the request url but the json data seems to work fine

CodePudding user response:

have you tried making the url f-string?

        response = requests.post(f'api{ids[0]}/pricing', cookies=cookies, headers=headers, json=post_data)
  • Related