exporting data using python script | XM Community
Question

exporting data using python script

  • 21 September 2020
  • 2 replies
  • 431 views

Userlevel 1
Badge +4

Hi
I found this python script to export survey data, it only export completed records
1- how can I configure it to export completed and in progress records
2- how can I configure it to Recode seen but unanswered multi-value fields as 0

Thank you


2 replies

Userlevel 6
Badge +51

Hello Isam
Which is the code? I can not see it :)

Userlevel 1
Badge +4

here is the code: it only export completed cases
-------------------
import sys
import requests
import zipfile, io


apiToken = "xxxxxxxxxxxxxxxxx" 
surveyId = "xxxxxxxxxxxxxxx"
dataCenter = 'xxxxxxxxxxxxx'

# Setting static parameters
requestCheckProgress = 0.0
progressStatus = "inProgress"
url = "https://{0}.qualtrics.com/API/v3/surveys/{1}/export-responses/".format(dataCenter, surveyId)
headers = {
  "content-type": "application/json",
  "x-api-token": apiToken,
  }

# Step 1: Creating Data Export
data = {
    "format": "spss",
    "seenUnansweredRecode": -9
    }

downloadRequestResponse = requests.request("POST", url, json=data, headers=headers)
print(downloadRequestResponse.json())

try:
  progressId = downloadRequestResponse.json()["result"]["progressId"]
except KeyError:
  print(downloadRequestResponse.json())
  sys.exit(2)
   
isFile = None

# Step 2: Checking on Data Export Progress and waiting until export is ready
while progressStatus != "complete" and progressStatus != "failed" and isFile is None:
  if isFile is None:
    print ("file not ready")
  else:
    print ("progressStatus=", progressStatus)
  requestCheckUrl = url + progressId
  requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers)
  try:
    isFile = requestCheckResponse.json()["result"]["fileId"]
  except KeyError:
    1==1
  print(requestCheckResponse.json())
  requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"]
  print("Download is " + str(requestCheckProgress) + " complete")
  progressStatus = requestCheckResponse.json()["result"]["status"]

#step 2.1: Check for error
if progressStatus is "failed":
  raise Exception("export failed")

fileId = requestCheckResponse.json()["result"]["fileId"]

# Step 3: Downloading file
requestDownloadUrl = url + fileId + '/file'
requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True)

# Step 4: Unzipping the file
zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall("MyQualtricsDownload")
print('Complete')

Leave a Reply