将数据输入到字典中

问题描述:

我尝试使用包含我自己的练习数据的我的typeform中的导入数据。包typeform使用typeforms API来下载数据。我想将我的回答放在字典中,以便我可以使用熊猫来分析数据。将数据输入到字典中

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

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 10: ordinal not in range(128)

我的第二个问题是,我不知道如何为遍历.questions.answersresponse以建设做一本字典。文档循环并打印出答案,但我想要在字典中。我想我可以从一个空字典开始,并使用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因此,如果有另一个包,比typeformbeautifulsoup也许我可以使用那个从我的反应去一个不错的熊猫数据框?

typeform包在Python 2中似乎没有像预期的那样工作 - 所以这是typeforms错误。

但是,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))