python - Qualtrics API - 某些列从字符串变为数字
问题描述
我正在从我们在 Qualtrics 进行的一项调查中下载数据。我找到了 Python 3 示例代码 ( https://api.qualtrics.com/docs/response-exports ) 并将我的代码基于它。
成功下载后,我注意到提供了选项列表供用户选择的问题以数字形式下载(我怀疑所选答案的索引)。
我相信答案很简单,只需输入一些参数以不同方式下载数据,但我似乎无法在 Qualtric 的 API 文档中找到它。
这是我的 api 凭据更改的代码:
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
downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '"}'
downloadRequestResponse = requests.request("POST", downloadRequestUrl, data=downloadRequestPayload, headers=headers)
progressId = downloadRequestResponse.json()["result"]["id"]
print(downloadRequestResponse.text)
#print (requests)
# Step 2: Checking on Data Export Progress and waiting until export is ready
while requestCheckProgress < 100 and progressStatus is not "complete":
requestCheckUrl = baseUrl + progressId
requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers)
requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"]
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')
谢谢 :)
解决方案
终于想通了。是的,需要添加 useLabels 参数,但是使用我上面提供的代码是不够的。出于某种原因 JSON 不会接受它,我相信它需要一个布尔值来类型转换为 JSON,而不仅仅是一个看起来像布尔值的字符串。所以我制作了一个字典并加载了参数并在字典上使用了 JSON.dumps() ,它就像一个魅力。希望这可以帮助其他任何想要他们的标签数据的人。
useLabels = True
dictionaryPayload = {'format': fileFormat, 'surveyId': surveyId, 'useLabels': useLabels}
downloadRequestPayload = json.dumps(dictionaryPayload)
推荐阅读
- mysql - 如何防止连接表中的组合或排列?
- apache-nifi - Nifi/Minifi:数据流的速率超过了出处记录速率。放慢流量以适应
- c# - 为什么即使切换到开发环境,我仍然会收到此消息?
- reactjs - 如何使用弹出对话框和 redux saga 等待成功登录 firebase
- labview - 如何在 LabVIEW 中准备好程序?
- php - 在 PhpStorm IDE 的 docker 容器中使用 Xdebug 调试 PHP 脚本
- node.js - NodeJS - 在文件之间传递一个变量,但该变量需要更新
- ios - 在 CoreData 的文本字段中搜索单词组
- php - 如何使用 CI4 解析器服务从模块渲染视图
- json - 该值是无效的 put 请求