如何操作GoogleADSAPI的枚举类的对象-python
我正在使用python客户端库连接到GoogleADS的API如何操作GoogleADSAPI的枚举类的对象-python,python,enums,google-api,google-ads-api,Python,Enums,Google Api,Google Ads Api,我正在使用python客户端库连接到GoogleADS的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 \''+f
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
}
]
}
为什么我得到的结果[类型]是一个整数?当我检查回溯调用时,我可以看到一个字符串:
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
}
我在上搜索了,发现这是因为:advisting\u channel\u type字段的数据类型为:Enum。如何操作Enum类的这个对象来获取字符串值?他们的文档中没有关于这方面的有用信息
请帮忙 通过创建一个列表来解决这个问题
lookup_list = ['DISPLAY', 'HOTEL', 'SEARCH', 'SHOPPING', 'UNKNOWN', 'UNSPECIFIED', 'VIDEO']
并将最后一行中的作业更改为
info['type'] = lookup_list[row.campaign.advertising_channel_type]
枚举提供了一些在索引和字符串之间转换的方法
频道类型=客户端服务。获取类型“AdvertisingChannelTypeEnum”
频道类型。广告频道类型。值“搜索”
=> 2
频道类型。广告频道类型。名称2
=>“搜索”
这是通过查看文档字符串发现的,例如
channel_types.AdvertisingChannelType.__doc__
# => 'A utility for finding the names of enum values.'
但是我怎么知道哪个索引对应于哪个字符串呢?他们的文件中没有此类信息。根据我的例子的结果,我只知道SEARCH==2,DISPLAY==3。谢谢希思,这对我很有用。如果您想获取所有可能的值并进行映射,而不是每次调用服务,那么可以使用:channel_types.adverisingchanneltype.items,它将返回一个元组列表,其中包含对,多亏了您和我。工作得很好。并感谢@Heath Winning提供有关您如何发现此信息的更多信息。文档中的示例仍然很少,这是一个救星!