使用python从函数中获取值

时间:2013-08-24 04:55:36

标签: python json function key-value firebase

我有一个python函数。

def v():
    response = urllib2.urlopen('https://api.gosquared.com/v2/concurrents?api_key=xxxxx&site_token=xxxx&presenter=old')
    data = json.load(response) 
    print data

我有一个firebase

f = Firebase('https://xxxxx.firebaseio.com/channel/1/status/viewers')
r = f.update({'viewers': 'v()'})

我希望该键的值:值对是v()函数的返回值。

我试过'v()',“v()”和v()无济于事。

我不理解的是什么?

2 个答案:

答案 0 :(得分:3)

你的函数什么也没有返回。请尝试使用return data代替(或之后)print data

答案 1 :(得分:3)

您的函数应该return一个值。它只是打印而不是返回nything

试试这个:

def v():
    response = urllib2.urlopen('https://api.gosquared.com/v2/concurrents?api_key=xxxxx&site_token=xxxx&presenter=old')
    data = json.load(response) 
    #comment the below line if you don't want to print data and simply want to return it
    print data
    return data

<强>更新: 如果要将返回的数据存储到变量中,则:

returned_data = v()