Qualtrics API - 某些列从字符串更改为数字

时间:2018-05-25 04:18:29

标签: python python-3.x extraction qualtrics

我正在从我们在Qualtrics中进行的调查中下载数据。我找到了Python 3示例代码(https://api.qualtrics.com/docs/response-exports)并基于它编写了代码。

成功下载后,我注意到提供了用户可供选择的选项列表的问题被下载为数字(我怀疑所选答案的索引)。

我认为答案很简单,因为输入一些参数来下载数据的方式不同,但我似乎无法在Qualtric的API文档中找到它。

This is what the data looks like when I use this program

This is what I want it to look like

这是我的代码,我的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')

谢谢:)

2 个答案:

答案 0 :(得分:1)

终于明白了。是的,需要添加useLabels参数,但使用我上面提供的代码还不够。出于某种原因,JSON不会接受它,我相信它需要一个布尔值来转换为JSON而不仅仅是一个看起来像布尔值的字符串。所以我创建了一个字典并加载了params并在字典上使用了JSON.dumps(),它就像一个魅力。希望这可以帮助其他人想要他们的数据与标签。

useLabels = True
dictionaryPayload = {'format': fileFormat, 'surveyId': surveyId, 'useLabels': useLabels}
downloadRequestPayload = json.dumps(dictionaryPayload)

答案 1 :(得分:0)

将useLabels参数设置为true:https://api.qualtrics.com/docs/create-response-export