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

py49o6xq  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(94)

我一直在使用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)

我做错了什么?

ca1c2owp

ca1c2owp1#

for survey in lst:    
    get_qualtircs_survey(dir_save_survey=path, survey_id=survey)

“说明:由于每次下载一个调查id,因此您必须遍历id列表,将它们单独传递给函数,而不是一次全部传递。
我还发现这些功能非常好用:”

!pip install QualtricsAPI
from QualtricsAPI.Setup import Credentials
from QualtricsAPI.Survey import Responses
# and so on...

相关问题