【发布时间】:2020-01-16 22:48:11
【问题描述】:
Google Ads API,他们的新版 Google Ads API 以一种名为“GoogleAdsRow”的格式返回数据。我根本没有发现以这种格式呈现的信息有任何用处,而且非常令人困惑。我想将我的文件打印为基本的 .csv 格式,但据我所知,没有任何类型的分隔符。到目前为止,我只能将每一整行打印为一个单元格。没有帮助:-)。
我的主要功能如下所示。我提供了两个示例,说明我尝试过的注释块标识了这两种尝试:
def main(client, customer_id, page_size):
ga_service = client.get_service('GoogleAdsService', version='v2')
query = ('SELECT ad_group.id, ad_group_criterion.type, '
'ad_group_criterion.criterion_id, '
'ad_group_criterion.keyword.text, '
'ad_group_criterion.keyword.match_type FROM ad_group_criterion '
'WHERE ad_group_criterion.type = KEYWORD')
results = ga_service.search(customer_id, query=query, page_size=page_size)
try:
with open(path, "w", encoding = "utf-8", newline = "") as f:
#with open(path, "w") as csv:
csv_writer = csv.writer(f, delimiter=',')
for row in results:
campaign = row.campaign
csv_writer.writerow([row]) #Prints entire returned row as a single cell
csv_writer.writerow(row) #Tells me there is no delimiter
可迭代错误如下
<ipython-input-15-e736ee2d05c9> in main(client, customer_id, page_size)
17 campaign = row.campaign
18 #csv_writer.writerow([row]) #Prints entire returned row as a single cell
---> 19 csv_writer.writerow(row) #Tells me there is no delimiter
20
21
Error: iterable expected, not GoogleAdsRow
【问题讨论】: