循环更新API中的价格

huangapple go评论53阅读模式
英文:

Loop for updating prices in an API

问题

我正在从电子表格中导入数据到一个包含我需要通过API发送PUT请求来更新电子商务中价格的产品信息的数据框中,但我遇到一个问题,我不知道如何创建循环以正确迭代所有数据框。

这是一个示例:

产品编号 变种编号 价格
id001 v0101 100
id002 v0201 120
id003 v0301 110
id003 v0302 115
id004 v0401 120
id005 130

方法1:

如果产品没有变种:

url = "一些API网址" + str(product_id)

body = {'price': int(price)}

token = token_key

payload = json.dumps(body)
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)

方法2:

如果产品有一个变种:

url = "一些API网址" + str(product_id)
body = {
    "variations": [{
	"id": str(variation_id),
	"price": int(price)
	}
   ]   
   }


token = token_key

payload = json.dumps(body)
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)

方法3:

如果product_id有两个或更多变种,在同一个body中附加variations_id和价格:

url = "一些API网址" + str(product_id)


body =  {
    "variations": [{
                    "id": str(variations_id_1),
                    "price": int(price_1)
            },
            {
                    "id": str(variations_id_2),
                    "price": int(price_2)
            },
            {
                    "id": str(variations_id_3),
                    "price": int(price_3)
            }
         ]
    }

token = token_key

payload = json.dumps(body)
headers = {
 'Authorization': f'Bearer {token}',
 'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)

注意:如果一个产品有多个变种,我忽略其中的任何一个,那些被省略的变种会被删除,结果就是它们将从电子商务中消失。

如何迭代整个数据框,使每个product_id传递其相应的variations_id和价格到JSON主体,如果下一个product_id相同,则将variations_id和价格添加到当前主体,就像product_id "id003" 有两个变种一样?

英文:

I am importing data from a spreadsheet to a dataframe that contains product information that I need to send in a put request through an API to update prices in an e-commerce, but I have a problem I don't know how to create the loop to iterate all the dataframe correctly.

This is an example:

Product_Id Variations_id Price
id001 v0101 100
id002 v0201 120
id003 v0301 110
id003 v0302 115
id004 v0401 120
id005 130

Approach 1:

If the product doesn’t have variations:

url = "some_api_url" + str(product_id)

body = {‘price’: int(price)}

token = token_key

payload = json.dumps(body)
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)

Approach 2:

If the product has one variation:

url = "some_api_url" + str(product_id)
body = {
    "variations": [{
	"id": str(variation_id),
	"price": int(price)
	}
   ]   
   }


token = token_key

payload = json.dumps(body)
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)

Approach 3:

If the product_id has two or more variations append the variations_id and the price in the same body:

url = "some_api_url" + str(product_id)


body =  {
    "variations": [{
                    "id": str(variations_id_1),
                    "price": int(price_1)
            },
            {
                    "id": str(variations_id_2),
                    "price": int(price_2)
            },
            {
                    "id": str(variations_id_3),
                    "price": int(price_3)
            }
         ]
    }

token = token_key

payload = json.dumps(body)
headers = {
 'Authorization': f'Bearer {token}',
 'Content-Type': 'application/json'
}

response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)

Note: if a product has more than one variation and I omit any of them, those omitted variations are eliminated, and as a result, they are products that will disappear from the e-commerce.

How can I iterate all the dataframe that each of the product_id passes its respective variations_id and price in the JSON body and if the next product_id is the same add the variations_id and price to the current body like the product_id "id003" with two variations?

答案1

得分: 1

  • 使用 DataFrame.iterrows 并构建一个大的 body。只要从每行创建一个字典,您就不会错过任何变体。最好先按产品ID排序。
  • 或者使用 DataFrame.groupby,按产品ID分组;遍历这些分组并为每个分组构建一个 body,然后进行 put 操作。

将对象分成组

英文:

> How can I iterate all the dataframe that each of the product_id passing its respective variations_id and price in the JSON body

  • Use DataFrame.iterrows and build one large body. As long as you make a dictionary from each row you won't miss any variations. Might be a good idea to sort by product id first.
  • or use DataFrame.groupby, group by product ID; iterate over the groups and build a body for each group and put that.

Splitting an object into groups

huangapple
  • 本文由 发表于 2023年2月14日 09:40:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定