将SurveyMonkey调查中的所有调查响应都输入到CSV中

时间:2018-10-30 16:32:16

标签: python json surveymonkey

我正在尝试使用SurveyMonkey API收集调查答复。理想情况下,我将能够将响应及其元数据收集到csv中。我正在使用Python 3.6

我尝试使用solution outlined here,但此答案似乎已过时。请注意,我的某些调查页面没有任何问题,因此我进行了少量编辑。我的代码是:

import pandas as pd
import requests


headers={"Authorization": "Bearer %s" % MY_ACCESS_TOKEN,
                           "Content-Type": "application/json"}
url = "https://api.surveymonkey.net/v3/surveys/%s/details" % (my_survey_id)
response = requests.get(url, headers=headers)
survey_data = response.json()

answer_dict = {}
for page in survey_data['pages']:
    for question in page['questions']:
        # Rows, choices, other, and cols all make up the possible answers
        if question.get('answers'):
          answers = question['answers'].get('rows', [])\
            + question['answers'].get('choices', [])\
            + question['answers'].get('other', [])\
             + question['answers'].get('cols', [])

          for answer in answers:
              answer_dict[answer['id']] = answer

df = pd.DataFrame(answer_dict)
df.to_csv('output.csv')

我收到此错误:

Traceback (most recent call last):
  File "survey-monkey-api-testing.py", line 19, in <module>
    + question['answers'].get('cols', [])
TypeError: can only concatenate list (not "dict") to list

有没有办法以编程方式将调查响应获取到扁平化的JSON中?

0 个答案:

没有答案