如何从Python的MailChimp批处理操作中提取响应URL?

发布于 2025-01-28 01:23:00 字数 685 浏览 3 评论 0原文

我正在使用批处理操作通过MailChimp Marketing API要求数据。我会收到一个response_body_url,该一旦单击,在本地下载了GZPICPENT文件。

请求响应响应的请求_body_url返回一个json文件的tar档案。

但是我想在python上提取相同的内容,然后将其加载为JSON数组。 MailChimp分享了有关该指南的指南,但是他们使用了函数process_batch_archive,但未定义此功能是什么或其工作方式。

这是指南的链接:链接 谁能帮助我取得预期的结果?谢谢。

I am requesting data via Mailchimp Marketing API using Batch Operations. I receive a response_body_url which once clicked, downloads gzipped files locally.

A GET request to the response_body_url returns a gzipped tar archive of JSON files.

But I would want to extract the same on Python and load it as a JSON array.
Mailchimp has shared a guide about the same, but they have used a function process_batch_archive and have not defined what this function is or how it works.
enter image description here

Here is the link to the guide: Link
Can anyone help me achieve the expected result? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2025-02-04 01:23:00
import tarfile
def process_batch_archive():
    if fname.endswith("tar.gz"):
        tar = tarfile.open(fname, "r:gz")
        tar.extractall()
        tar.close()
    elif fname.endswith("tar"):
        tar = tarfile.open(fname, "r:")
        tar.extractall()
        tar.close()
import tarfile
def process_batch_archive():
    if fname.endswith("tar.gz"):
        tar = tarfile.open(fname, "r:gz")
        tar.extractall()
        tar.close()
    elif fname.endswith("tar"):
        tar = tarfile.open(fname, "r:")
        tar.extractall()
        tar.close()
难以启齿的温柔 2025-02-04 01:23:00

返回dict,因为GZPICTED存档通常包含几个文件。

import tarfile

def process_batch_archive(data):
    tar_fileobj = io.BytesIO(data)
    json_contents = {}
    with tarfile.open(fileobj=tar_fileobj, mode="r:gz") as tar:
        for file in tar:
            print (file.name, file.size)
            if file.name.endswith(".json"):
                contentfobj = tar.extractfile(file)
                json_contents[file.name] = json.load(contentfobj)
    return json_contents

x = process_batch_archive(data)
print(x.keys())

Returning a dict, since the gzipped archive typically contains few files.

import tarfile

def process_batch_archive(data):
    tar_fileobj = io.BytesIO(data)
    json_contents = {}
    with tarfile.open(fileobj=tar_fileobj, mode="r:gz") as tar:
        for file in tar:
            print (file.name, file.size)
            if file.name.endswith(".json"):
                contentfobj = tar.extractfile(file)
                json_contents[file.name] = json.load(contentfobj)
    return json_contents

x = process_batch_archive(data)
print(x.keys())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文