Qualtrics API问题:导出问卷数据时部分列显示数字而非选项文本如何解决?
How to Export Qualtrics Survey Responses with Option Text Instead of Numeric Indexes
我之前处理Qualtrics数据导出的时候也碰到过一模一样的问题——默认导出的选择题列是数字索引,而我们需要的是选项文本。其实Qualtrics API的响应导出接口里确实有一个参数可以实现这个需求,只是藏得有点隐蔽,就是useLabels参数,把它设为true就能解决你的问题。
具体修改步骤:
你只需要在创建导出请求的Payload里添加"useLabels": true这一项就行。原来的请求Payload只指定了格式和问卷ID,现在把它更新成包含标签设置的结构:
原来的代码段:
downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '"}'
修改后:
downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '","useLabels": true}'
原理说明:
当useLabels参数设置为true时,Qualtrics API会将选择题的选项文本(而不是对应的数字索引值)写入导出的CSV文件中,完全匹配你期望的输出样式。
修改后的完整代码
import shutil import os import requests import zipfile import json import io # Setting user Parameters apiToken = "myKey" surveyId = "mySurveyID" fileFormat = "csv" dataCenter = "az1" # Setting static parameters requestCheckProgress = 0 progressStatus = "in progress" baseUrl = "https://{0}.qualtrics.com/API/v3/responseexports/".format(dataCenter) headers = { "content-type": "application/json", "x-api-token": apiToken, } # Step 1: Creating Data Export downloadRequestUrl = baseUrl # 添加useLabels参数,导出选项文本而非索引 downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '","useLabels": true}' downloadRequestResponse = requests.request("POST", downloadRequestUrl, data=downloadRequestPayload, headers=headers) progressId = downloadRequestResponse.json()["result"]["id"] print(downloadRequestResponse.text) # Step 2: Checking on Data Export Progress and waiting until export is ready while requestCheckProgress < 100 and progressStatus != "complete": requestCheckUrl = baseUrl + progressId requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers) requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"] progressStatus = requestCheckResponse.json()["result"]["status"] # 补充更新状态,避免无限循环 print("Download is " + str(requestCheckProgress) + " complete") # Step 3: Downloading file requestDownloadUrl = baseUrl + progressId + '/file' requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True) # Step 4: Unzipping the file zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall("MyQualtricsDownload") # Step 5: Move the file out of the folder and place it in the working directory --> change the paths to the appropiate paths for the server shutil.move( "/Users/Abram/Documents/PCC/MyQualtricsDownload/Mindshare English v21.csv", "/Users/Abram/Documents/PCC/Mindshare English v21.csv") os.rmdir("/Users/Abram/Documents/PCC/MyQualtricsDownload/") print('Complete')
另外我还帮你补充了获取progressStatus的代码(原来的循环里没有更新这个变量,可能会导致无限循环),确保导出完成后能正常退出循环。导出完成后,你可以打开CSV文件检查,选择题的列应该已经显示对应的选项文本了,和你期望的样式一致。
内容的提问来源于stack exchange,提问作者Abram
相关产品推荐
相关产品推荐

