将API请求响应转换为List python

时间:2016-07-05 05:23:13

标签: python json api

我写了一个python API请求调用,脚本如下所示:

import requests  
from requests_oauthlib import OAuth1  
import json  

def PULL():  
    url = "someURL"   
    ConsumerKey = "someKey"  
    ConsumerSecret = "someSecret"  
    Token = "someToken"  
    TokenSecret = "someToken"  

    auth = OAuth1(ConsumerKey, ConsumerSecret, Token, TokenSecret)
    r = requests.request("GET", url, auth=auth)  
    data = r.json()  
    print data  

PULL()

然后我在我的Macbook Pro上使用了终端,我收到了以下回复:

{u'watermarked': 0, u'orientation': u'landscape', u'datePublished': u'2016-05-25T13:49:30Z', u'extension': [u'jpeg']}

如何将其转换为列表,以便数据如下所示?

watermarked: 0  
orientation: landscape  
datePublished: 2016-05-25T13:49:30Z   
extension: jpeg

1 个答案:

答案 0 :(得分:0)

当您在输出时没有大括号和u'...' unicode字符串文字时,不仅仅是print data,而是:

for key, value in data:
    print '%s: %s' % (key, value)

那应该给:

watermarked: 0
orientation: landscape
datePublished: 2016-05-25T13:49:30Z
extension: jpeg

关于输出(被认为是理所当然的命令并由" dict加载过程&#34确定)。