在带有 API 调用的字典中使用 .format()

Using .format() in a dictionary with an API call

我正在进行 api 调用,为此我必须每天手动更改有效负载中的 endDate。我不能在字典中使用 .format() 。有人可以帮忙吗?

当前负载:我正在手动更改结束日期

payload = "{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000+00:00\",\"***endDate\":\"2022-01-13***T00:00:00.0000000+00:00\",\"format\":\"csv\"}"

预期负载:

payload = "{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000+00:00\",\endDate\":\"{}T00:00:00.0000000+00:00\",\"format\":\"csv\"}".format(today)

今天是今天的变量

如果您的有效载荷是一个字符串,并且您只想使用特定变量对其进行格式化,您可以使用 f-strings,例如:

today = datetime.strftime(datetime.now(), "%Y-%m-%d")

payload = f"{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000+00:00\",\endDate\":\"{today}T00:00:00.0000000+00:00\",\"format\":\"csv\"}"

那是一个字符串,而不是字典,您想要的 .format 东西可以工作。猜测这实际上是 JSON 数据,做这种事情的正常方法是构建一个 python dict 并在以后序列化它。使用 python "f-string" 使得在字符串格式规范本身中调用函数变得简单。 datetime.datetime.utcnow() 给出当前的 UTC 时间。它可以转换为 date 并且其 isoformat 方法写入您想要的格式。所以,

import datetime as dt
import json

data = {
    "dimensions": ["AdsetId", "Adset", "CampaignId", "Campaign",
        "Device", "Day", "Month", "Year", "Week", "Os"],
    "metrics": ["AdvertiserCost", "Displays", "ClickThroughRate", 
        "Cpc", "AppInstalls", "Clicks"],
    "timezone": "UTC", 
    "advertiserIds": "69957", 
    "currency": "USD", 
    "startDate": "2022-01-01T00:00:00.0000000+00:00", 
    "endDate": f"{dt.datetime.utcnow().date().isoformat()}T00:00:00.0000000+00:00", 
    "format": "csv"}
    
    
payload = json.dumps(data)
print(payload)

在我看来,一种非常直接的方法是按照@Olvin Roght 在 中建议的方式进行,本质上是这样的:

  1. 使用 json.loads().
  2. 将有效负载转换为字典
  3. 修改字典中的"endDate"
  4. 将字典转换回字符串。

根本不涉及 .format()

from datetime import datetime, timezone
import json

payload = "{\"dimensions\":[\"AdsetId\",\"Adset\",\"CampaignId\",\"Campaign\",\"Device\",\"Day\",\"Month\",\"Year\",\"Week\",\"Os\"],\"metrics\":[\"AdvertiserCost\",\"Displays\",\"ClickThroughRate\",\"Cpc\",\"AppInstalls\",\"Clicks\"],\"timezone\":\"UTC\",\"advertiserIds\":\"69957\",\"currency\":\"USD\",\"startDate\":\"2022-01-01T00:00:00.0000000+00:00\",\"endDate\":\"2022-01-13T00:00:00.0000000+00:00\",\"format\":\"csv\"}"
payload = json.loads(payload)
dt = datetime.now(timezone.utc).replace(minute=0, hour=0, second=0, microsecond=0)
payload["endDate"] = dt.isoformat(timespec="microseconds")
print(json.dumps(payload, indent=4))

输出:

{
    "dimensions": [
        "AdsetId",
        "Adset",
        "CampaignId",
        "Campaign",
        "Device",
        "Day",
        "Month",
        "Year",
        "Week",
        "Os"
    ],
    "metrics": [
        "AdvertiserCost",
        "Displays",
        "ClickThroughRate",
        "Cpc",
        "AppInstalls",
        "Clicks"
    ],
    "timezone": "UTC",
    "advertiserIds": "69957",
    "currency": "USD",
    "startDate": "2022-01-01T00:00:00.0000000+00:00",
    "endDate": "2022-01-13T00:00:00.000000+00:00",
    "format": "csv"
}