在 Stripe API 中删除重复客户

问题描述 投票:0回答:1

我正在尝试编写代码来比较客户并删除重复的客户。

如何将每个客户的参数与filtered_customers中的其余参数进行比较并将其添加到列表中?

import stripe

stripe.api_key = "sk_test_51OQA44ESuCeG...hZce"

customers = stripe.Customer.list(limit=100) 
filtered_customers = [] 

for customer in customers: 
  
  if customer["id"] is not None:
    filtered_customers.append(customer) 
python list rest
1个回答
0
投票

有几种方法可以做到这一点,但有一些注意事项。如果客户的个人资料有重复,您将如何选择保留哪一个?您可能想要最新的。检查[客户API对象][1]“创建”的客户字典对象是一个unix时间戳,您可以使用它来过滤

customers = [
#your list of customers

]

# Dictionary to hold the most recent customer for each unique identifier
unique_customers = {}

for customer in customers:
    identifier = customer["email"]  # Using email as the unique identifier
  
#if identifier exists the  compare the 'created' timestamps of the 
#current customer and the one already stored in the dictionary unique customers.
#If the current customer was created more recently (has a larger 'created' timestamp),
# the customer in the dictionary is replaced.

    if identifier not in unique_customers or customer["created"] > unique_customers[identifier]["created"]:
        unique_customers[identifier] = customer

# The values in unique_customers are your filtered customers
filtered_customers = list(unique_customers.values())
print(filtered_customers)

    ```


  [1]: https://stripe.com/docs/api/customers/object?lang=python
© www.soinside.com 2019 - 2024. All rights reserved.