Python-Mailchimp批处理PUT请求 - python

我正在尝试对Mailchimp API使用批处理功能。我当前的设置是这样的

operations = []
for idx, row in users_df.iterows():
    payload = {
        'email': row['email'],
        'last_updated': row['new_time'],
        'custom_value': row['new_value']
    }
    operation_item = {
        "method": "POST", # I changed this to PUT
        "path": '/lists/members/12345',
        "body": json.dumps(payload),
    }
    operations.append(operation_item)
client = mc.MailChimp(MAILCHIMP_TOKEN, MAILCHIMP_USER)
batch = client.batches.create(data={"operations": operations})

每当我使用POST方法时,都会出现此错误:[email protected] is already a list member. Use PUT to insert or update list members.

但是,每当我将方法更改为PUT时,都会出现另一个错误:The requested method and resource are not compatible. See the Allow header for this resource's available methods.

有办法克服吗?我已经看过this,而this在Ruby中是一个类似的问题。

参考方案

您不能对列表资源"/lists/members/12345"进行PUT操作,需要将路径更改为"/lists/members/12345/{email}"

该代码对我们有效:

def add_users_to_mailchimp_list(list_id, users):
operations = []
client = get_mailchimp_client()

for user in users:
    member = {
        'email_address': user.email,
        'status_if_new': 'subscribed',
        'merge_fields': {
            'FNAME': user.first_name or '',
            'LNAME': user.last_name or '',
        },
    }
    operation_item = {
        "method": "PUT",
        "path": client.lists.members._build_path(list_id, 'members', user.email),
        "body": json.dumps(member),
    }
    operations.append(operation_item)
return client.batches.create(data={"operations": operations})

我想使用受保护的方法client.lists.members._build_path有点hacky,如果您希望可以手动构建url,它将为f'lists/{list_id}/members/{user.email}'

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…