我在循环中查询API时出现问题,其中字符串索引必须是整数,而不是str。
我想知道是否有人可以就下面的错误提出建议?
所以,这是错误:
for
基本上,我所做的是使用下面的audience_stats()函数循环遍历single_audience_stats()函数。
/home/slauw/lotame_api_test/lotame_api_functions.pyc in audience_stats(config_filepath, date_today)
185 print("aud_stats begun, current row length = " + str(len(aud_stats.index)))
186 for i in ids:
--> 187 current_aud_stats = single_audience_stats(config_filepath, i, date_today)
188 aud_stats = aud_stats.append(current_aud_stats)
189 print("id " + i + " added, current row length = " + str(len(aud_stats.index)))
/home/slauw/lotame_api_test/lotame_api_functions.pyc in single_audience_stats(config_filepath, audience_id, date_today)
151 query_string = "statistics/audiences/" + audience_id + "?client_as_group=true&date_range=LAST_30_DAYS&generate_stats=now&device_graph=false&page_count=100&page_num=1&sort_attr=data_date&sort_order=ASC"
152 query_result = get_query(query_string, config_filepath)
--> 153 audienceStats = query_result["audienceStatsReports"]
154 statsTable = []
155 a_obj = {}
Config_filepath基本上只是登录API的身份验证,而date_today是通过time.strftime(“%Y-%m-%d)”创建的。
get_query()函数基本上是使用配置文件帮助连接API的函数。
- 更新:所以在query_result结束时,我会得到'dict'类型。
以下是它的样子:
def audience_stats(config_filepath, date_today):
id_query_string = "audiences?page_count=500&page_num=1&sort_order=ASC&sort_attr=id&or=true"
id_query_result = get_query(id_query_string, config_filepath)
audiences = id_query_result["Audience"]
first_id = audiences[0]["id"]
print("first id: " + first_id)
audiences = audiences[1:]
ids = []
for a in audiences:
ids.append(a["id"])
print("ids: " + str(ids))
aud_stats = single_audience_stats(config_filepath, first_id, date_today)
print("aud_stats begun, current row length = " + str(len(aud_stats.index)))
for i in ids:
current_aud_stats = single_audience_stats(config_filepath, i, date_today)
aud_stats = aud_stats.append(current_aud_stats)
print("id " + i + " added, current row length = " + str(len(aud_stats.index)))
print("loop complete")
return aud_stats
def single_audience_stats(config_filepath, audience_id, date_today):
query_string = "statistics/audiences/" + audience_id + "?client_as_group=true&date_range=LAST_30_DAYS&generate_stats=now&device_graph=false&page_count=100&page_num=1&sort_attr=data_date&sort_order=ASC"
query_result = get_query(query_string, config_filepath)
audienceStats = query_result["audienceStatsReports"]
statsTable = []
a_obj = {}
keys = ["audienceId", "audienceName", "averageDailyUsers", "uniques", "pageViews"]
if (len(audienceStats) > 0):
a = audienceStats[0]
for key in keys:
if key in a.keys():
a_obj[key] = a[key]
else:
a_obj[key] = ""
else:
for key in keys:
a_obj[key] = ""
a_obj["date"] = date_today
statsTable.append(a_obj)
stats_df = pd.DataFrame(statsTable)
return stats_df
任何人都可以帮助解决上述问题吗?