在 Python 中将列表作为函数参数传递,以从 Qualtrics 下载多个调查

问题描述 投票:0回答:1

我一直在使用 this 从我的 Qualtrics 管理门户中提取 Qualtrics 调查。这段代码完全工作正常,但我的问题是我想在调查 ID 列表中使用这段代码,然后逐个下载每个文件。

我怎样才能实现这个目标?

我从上面的链接使用的代码是:

def get_qualtrics_survey(dir_save_survey, survey_id):
   # Setting user Parameters
   api_token = "token"
   file_format = "csv"
   data_center = 'xx' # "<Organization ID>.<Datacenter ID>"

# Setting static parameters
request_check_progress = 0
progress_status = "in progress"
base_url = "https://{0}.qualtrics.com/API/v3/responseexports/".format(data_center)
headers = {
    "content-type": "application/json",
    "x-api-token": api_token,
}

# Step 1: Creating Data Export
download_request_url = base_url
download_request_payload = '{"format":"' + file_format + '","surveyId":"' + survey_id + '"}' # you can set useLabels:True to get responses in text format
download_request_response = requests.request("POST", download_request_url, data=download_request_payload, headers=headers)
progress_id = download_request_response.json()["result"]["id"]
# print(download_request_response.text)

# Step 2: Checking on Data Export Progress and waiting until export is ready
while request_check_progress < 100 and progress_status != "complete":
    request_check_url = base_url + progress_id
    request_check_response = requests.request("GET", request_check_url, headers=headers)
    request_check_progress = request_check_response.json()["result"]["percentComplete"]

# Step 3: Downloading file
request_download_url = base_url + progress_id + '/file'
request_download = requests.request("GET", request_download_url, headers=headers, stream=True)

# Step 4: Unzipping the file
zipfile.ZipFile(io.BytesIO(request_download.content)).extractall(dir_save_survey)
print('Downloaded qualtrics survey')

## the below list contains the survey ids that I want to download
lst = ['1','2','3']
##calling the function
path = r"C:\Users\Downloads"
get_qualtrics_survey(dir_save_survey=path, survey_id = lst)

我在这里做错了什么?

python-3.x list for-loop qualtrics
1个回答
0
投票

lst = ['1','2','3'] 用于 lst 中的调查:
get_qualtircs_survey(dir_save_survey=路径,survey_id=调查)

© www.soinside.com 2019 - 2024. All rights reserved.