将数据输入到字典中

时间:2017-11-11 10:21:51

标签: python forms typeform

我尝试使用包含我自己的运动数据的我的typeform中的导入数据。包typeform使用typeforms API下载数据。我想将我的回复放在字典中,以便我可以使用pandas来分析数据。

package documentation很短,它提供了6行有关如何导入数据的代码。我的第一个问题是我得到一个错误,我知道如何处理

  

UnicodeEncodeError:' ascii'编解码器不能对字符u' \ xe5'进行编码。位置10:序数不在范围内(128)

我的第二个问题是,我不确定如何在.questions.answers response上执行for循环以构建字典。文档循环并打印出答案,但我想在字典中。我想我可以从一个空字典开始并使用append方法。

以下是我对包文档页面上代码的修改:

import typeform

# set my parameters and download the form data
apikey = '07466391d081290e87a95868893868e6e55c73c3'
formid = 'xqBhCO'
form = typeform.Form(api_key=apikey, form_id=formid)

# Fetch all responses to the form with default options
responses = form.get_responses()
responses # I have 16 questions and 138 responses 

# Print '<question>: <answer>' for all responses to this form
for response in responses:
    for answer in response.answers:
        print '{question}: {answer}'.format(question=answer.question,
        answer=answer.answer)

运行此代码会产生输出:

  ... 
  Yoga: 1
  date: 2017-11-10
  Traceback (most recent call last):
    File "<stdin>", line 3, in <module>
  UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 10: 
ordinal not in range(128)

更新

阅读了typeforms API文档后,我看到我可以使用

https://api.typeform.com/v1/form/[typeform_UID]?key=[your_API_key]

为了查看数据,我的情况是https://api.typeform.com/v1/form/xqBhCO?key=07466391d081290e87a95868893868e6e55c73c3因此,如果有另一个包,则比typeform更加已知/稳定,例如beautifulsoup也许我可以使用那个从我的回答到一个漂亮的熊猫数据帧?

1 个答案:

答案 0 :(得分:0)

在Python 2中,typeform包似乎没有按预期工作 - 因此这是字体错误。

然而,typeform包 - 以及您的示例代码 - 适用于Python 3。

(Unicode字符串处理在Python 3中发生了很大变化)

import typeform

# set my parameters and download the form data
apikey = '07466391d081290e87a95868893868e6e55c73c3'
formid = 'xqBhCO'
form = typeform.Form(api_key=apikey, form_id=formid)

# Fetch all responses to the form with default options
responses = form.get_responses()
responses # I have 16 questions and 138 responses 

# Print '<question>: <answer>' for all responses to this form
for response in responses:
    for answer in response.answers:
        print('{question}: {answer}'.format(question=answer.question,
        answer=answer.answer))