python - 如何操作 Google Ads API 的 Enum 类的对象 - python

标签 python enums google-api google-ads-api

我正在使用 python 客户端库连接到 Google Ads 的 API。

    ga_service = client_service.get_service('GoogleAdsService')
    query = ('SELECT campaign.id, campaign.name, campaign.advertising_channel_type '
            'FROM campaign WHERE date BETWEEN \''+fecha+'\' AND \''+fecha+'\'')

    response = ga_service.search(<client_id>, query=query,page_size=1000)
    result = {}
    result['campanas'] = []

    try:
        for row in response:
            print row
            info = {}
            info['id'] = row.campaign.id.value
            info['name'] = row.campaign.name.value
            info['type'] = row.campaign.advertising_channel_type

当我解析值时,这是我得到的结果:

{
  "campanas": [
    {
      "id": <campaign_id>, 
      "name": "Lanzamiento SIKU", 
      "type": 2
    }, 
    {
      "id": <campaign_id>, 
      "name": "lvl1 - website traffic", 
      "type": 2
    }, 
    {
      "id": <campaign_id>, 
      "name": "Lvl 2 - display", 
      "type": 3
    }
  ]
}

为什么我得到 result["type"] 的整数?当我检查回溯调用时,我可以看到一个字符串:

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 397083380
  }
  name {
    value: "Lanzamiento SIKU"
  }
  advertising_channel_type: SEARCH
}

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 1590766475
  }
  name {
    value: "lvl1 - website traffic"
  }
  advertising_channel_type: SEARCH
}

campaign {
  resource_name: "customers/<customer_id>/campaigns/<campaign_id>"
  id {
    value: 1590784940
  }
  name {
    value: "Lvl 2 - display"
  }
  advertising_channel_type: DISPLAY
}

我在 Documentation for the API 上搜索过发现是因为字段:advertising_channel_type 是数据类型:枚举。如何操作 Enum 类的这个对象来获取字符串值?在他们的文档中没有关于此的有用信息。

请帮忙!!

最佳答案

Enum 带有一些在索引和字符串之间转换的方法

channel_types = client_service.get_type('AdvertisingChannelTypeEnum')

channel_types.AdvertisingChannelType.Value('SEARCH')
# => 2
channel_types.AdvertisingChannelType.Name(2)
# => 'SEARCH'

这是通过查看文档字符串发现的,例如

channel_types.AdvertisingChannelType.__doc__
# => 'A utility for finding the names of enum values.'

关于python - 如何操作 Google Ads API 的 Enum 类的对象 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53089024/

相关文章:

python - 将 pandas 时间戳重置为每月的第一天

java - 枚举作为 Spark PairRDD 导致问题的关键

java - 在 EnumSet 和 boolean 值数组之间转换

python - 从构造函数中选择枚举值?

javascript - 是否可以仅使用公钥修改公共(public)谷歌日历中的事件数据?

youtube - YouTube上传小部件时间

python - 使用 Python 3.6 创建/使用文件

python - 基本 gRPC 版本不工作

python - 太多值无法在python中解压缩?

python - 使用 Google People API 的 REST people.get() 中的 userId 参数无效