【问题标题】:How to manipulate an object of Google Ads API's Enum class - python如何操作 Google Ads API 的 Enum 类的对象 - python
【发布时间】:2018-10-31 17:28:07
【问题描述】:

我正在使用 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 类的这个对象来获取字符串值?他们的文档中没有关于此的有用信息。

请帮忙!!

【问题讨论】:

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


    【解决方案1】:

    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.'
    

    【讨论】:

    • 谢谢希思,这对我有用。如果您想获取所有可能的值并进行映射而不是每次都调用服务,您可以使用:channel_types.AdvertisingChannelType.items(),它将返回带有对的元组列表
    • 感谢你们两位。工作完美。感谢@Heath-Winning 提供有关您如何找到此内容的更多信息。文档中的示例仍然很少,这是救命稻草!
    【解决方案2】:

    我认为最好的方法是这样一行代码:

    import proto
    row_dict = proto.Message.to_dict(google_ads_row, use_integers_for_enums=False)
    

    这将一次性将整个 google ads 行转换为字典,并自动获取 ENUM 值而不是数字。

    【讨论】:

    • 它给出的错误proto module not found, protobuf is already installed.
    • 也尝试安装这些:googleapis-common-protos, proto-plus @VijaysinhParmar
    【解决方案3】:

    @Vijaysinh Parmar 尝试关注

    from google.protobuf import json_format
    
    row_dict = json_format.MessageToJson(row, use_integers_for_enums=False)
    

    【讨论】:

      【解决方案4】:

      只是解决它,创建一个列表

      lookup_list = ['DISPLAY', 'HOTEL', 'SEARCH', 'SHOPPING', 'UNKNOWN', 'UNSPECIFIED', 'VIDEO']
      

      并将最后一行中的分配更改为

      info['type'] = lookup_list[row.campaign.advertising_channel_type]
      

      【讨论】:

      • 但是我怎么知道哪个索引对应哪个字符串呢?他们的文档中没有此类信息。根据我的示例结果,我所知道的只是 SEARCH == 2 和 DISPLAY == 3。
      猜你喜欢
      • 2021-08-13
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2021-05-12
      相关资源
      最近更新 更多