循环更新API中的价格

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

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:

如果产品没有变种:

  1. url = "一些API网址" + str(product_id)
  2. body = {'price': int(price)}
  3. token = token_key
  4. payload = json.dumps(body)
  5. headers = {
  6. 'Authorization': f'Bearer {token}',
  7. 'Content-Type': 'application/json'
  8. }
  9. response = requests.request("PUT", url, headers=headers, data=payload)
  10. print(response.text)

方法2:

如果产品有一个变种:

  1. url = "一些API网址" + str(product_id)
  2. body = {
  3. "variations": [{
  4. "id": str(variation_id),
  5. "price": int(price)
  6. }
  7. ]
  8. }
  9. token = token_key
  10. payload = json.dumps(body)
  11. headers = {
  12. 'Authorization': f'Bearer {token}',
  13. 'Content-Type': 'application/json'
  14. }
  15. response = requests.request("PUT", url, headers=headers, data=payload)
  16. print(response.text)

方法3:

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

  1. url = "一些API网址" + str(product_id)
  2. body = {
  3. "variations": [{
  4. "id": str(variations_id_1),
  5. "price": int(price_1)
  6. },
  7. {
  8. "id": str(variations_id_2),
  9. "price": int(price_2)
  10. },
  11. {
  12. "id": str(variations_id_3),
  13. "price": int(price_3)
  14. }
  15. ]
  16. }
  17. token = token_key
  18. payload = json.dumps(body)
  19. headers = {
  20. 'Authorization': f'Bearer {token}',
  21. 'Content-Type': 'application/json'
  22. }
  23. response = requests.request("PUT", url, headers=headers, data=payload)
  24. 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:

  1. url = "some_api_url" + str(product_id)
  2. body = {‘price’: int(price)}
  3. token = token_key
  4. payload = json.dumps(body)
  5. headers = {
  6. 'Authorization': f'Bearer {token}',
  7. 'Content-Type': 'application/json'
  8. }
  9. response = requests.request("PUT", url, headers=headers, data=payload)
  10. print(response.text)

Approach 2:

If the product has one variation:

  1. url = "some_api_url" + str(product_id)
  2. body = {
  3. "variations": [{
  4. "id": str(variation_id),
  5. "price": int(price)
  6. }
  7. ]
  8. }
  9. token = token_key
  10. payload = json.dumps(body)
  11. headers = {
  12. 'Authorization': f'Bearer {token}',
  13. 'Content-Type': 'application/json'
  14. }
  15. response = requests.request("PUT", url, headers=headers, data=payload)
  16. 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:

  1. url = "some_api_url" + str(product_id)
  2. body = {
  3. "variations": [{
  4. "id": str(variations_id_1),
  5. "price": int(price_1)
  6. },
  7. {
  8. "id": str(variations_id_2),
  9. "price": int(price_2)
  10. },
  11. {
  12. "id": str(variations_id_3),
  13. "price": int(price_3)
  14. }
  15. ]
  16. }
  17. token = token_key
  18. payload = json.dumps(body)
  19. headers = {
  20. 'Authorization': f'Bearer {token}',
  21. 'Content-Type': 'application/json'
  22. }
  23. response = requests.request("PUT", url, headers=headers, data=payload)
  24. 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:

确定