Python 打印对e GET请求的JSON响应中的多个条目

Python 打印对e GET请求的JSON响应中的多个条目,python,json,Python,Json,这是我的第一篇文章,我是python新手。我一直在玩弄一副纸牌API。我试图做一个基本的脚本,在那里我打印我从牌堆中画的三张牌。我只能让它打印最后一张卡两次。我试图打印三种不同的“价值”和“套装”。请参阅我的GET请求的JSON输出: { "success": true, "deck_id": "jj1qy9tz9fkj", "cards": [ { &

这是我的第一篇文章,我是python新手。我一直在玩弄一副纸牌API。我试图做一个基本的脚本,在那里我打印我从牌堆中画的三张牌。我只能让它打印最后一张卡两次。我试图打印三种不同的“价值”和“套装”。请参阅我的GET请求的JSON输出:

{
    "success": true,
    "deck_id": "jj1qy9tz9fkj",
    "cards": [
        {
            "code": "2H",
            "image": "https://deckofcardsapi.com/static/img/2H.png",
            "images": {
                "svg": "https://deckofcardsapi.com/static/img/2H.svg",
                "png": "https://deckofcardsapi.com/static/img/2H.png"
            },
            "value": "2",
            "suit": "HEARTS"
        },
        {
            "code": "KC",
            "image": "https://deckofcardsapi.com/static/img/KC.png",
            "images": {
                "svg": "https://deckofcardsapi.com/static/img/KC.svg",
                "png": "https://deckofcardsapi.com/static/img/KC.png"
            },
            "value": "KING",
            "suit": "CLUBS"
        },
        {
            "code": "QH",
            "image": "https://deckofcardsapi.com/static/img/QH.png",
            "images": {
                "svg": "https://deckofcardsapi.com/static/img/QH.svg",
                "png": "https://deckofcardsapi.com/static/img/QH.png"
            },
            "value": "QUEEN",
            "suit": "HEARTS"
        }
    ],
    "remaining": 49
}
这是我的剧本:

import requests


url = "https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1"

payload = {}
headers = {
  'Cookie': '__cfduid=xxxxxxxxxxx; Cookie_1=value'
}

response = requests.request("GET", url, headers=headers, data = payload)

print(response.text)
deck = response.json()
deck_id = deck['deck_id']
print(deck_id)

url = "https://deckofcardsapi.com/api/deck/" + deck_id + "/draw/?count=3"



response = requests.request("GET", url, headers=headers, data = payload)

print(response.text)
response_data = response.json()
for element in response_data['cards']:
    card1 = element['value']
    suit1 = element['suit']
for element in response_data['cards']:
    card2 = element['value']
    suit2 = element['suit']
for element in response_data['cards']:
    card3 = element['value']
    suit3 = element['suit']
print(str(card1) + ' of ' + str(suit1))
print(str(card2) + ' of ' + str(suit2))
print(str(card3) + ' of ' + str(suit3))
remaining = response.json()
card_remaining = remaining['remaining']
print('There are ' + str(card_remaining) + ' cards remaining')

我不确定你在这里要做什么,但我想你说你是python新手:

for element in response_data['cards']:
    card1 = element['value']
    suit1 = element['suit']
for element in response_data['cards']:
    card2 = element['value']
    suit2 = element['suit']
for element in response_data['cards']:
    card3 = element['value']
    suit3 = element['suit']
print(str(card1) + ' of ' + str(suit1))
print(str(card2) + ' of ' + str(suit2))
print(str(card3) + ' of ' + str(suit3))
remaining = response.json()
card_remaining = remaining['remaining']
您应该划掉前面提到的几行,并将其替换为以下内容:

for element in response_data['cards']:
    print(element['value'] + ' of ' + element['suit'])
card_remaining = response_data['remaining']

我不确定你在这里要做什么,但我想你说你是python新手:

for element in response_data['cards']:
    card1 = element['value']
    suit1 = element['suit']
for element in response_data['cards']:
    card2 = element['value']
    suit2 = element['suit']
for element in response_data['cards']:
    card3 = element['value']
    suit3 = element['suit']
print(str(card1) + ' of ' + str(suit1))
print(str(card2) + ' of ' + str(suit2))
print(str(card3) + ' of ' + str(suit3))
remaining = response.json()
card_remaining = remaining['remaining']
您应该划掉前面提到的几行,并将其替换为以下内容:

for element in response_data['cards']:
    print(element['value'] + ' of ' + element['suit'])
card_remaining = response_data['remaining']

这是一份字典清单。您只需遍历它并提取所需的元素

data ={
"success": True,
"deck_id": "jj1qy9tz9fkj",
"cards": [
    {
        "code": "2H",
        "image": "https://deckofcardsapi.com/static/img/2H.png",
        "images": {
            "svg": "https://deckofcardsapi.com/static/img/2H.svg",
            "png": "https://deckofcardsapi.com/static/img/2H.png"
        },
        "value": "2",
        "suit": "HEARTS"
    },
    {
        "code": "KC",
        "image": "https://deckofcardsapi.com/static/img/KC.png",
        "images": {
            "svg": "https://deckofcardsapi.com/static/img/KC.svg",
            "png": "https://deckofcardsapi.com/static/img/KC.png"
        },
        "value": "KING",
        "suit": "CLUBS"
    },
    {
        "code": "QH",
        "image": "https://deckofcardsapi.com/static/img/QH.png",
        "images": {
            "svg": "https://deckofcardsapi.com/static/img/QH.svg",
            "png": "https://deckofcardsapi.com/static/img/QH.png"
        },
        "value": "QUEEN",
        "suit": "HEARTS"
    }
],
"remaining": 49
}

# iterate through your list
for el in data.get('cards'):
    print('value: ', el.get('value'))
    print('suit: ', el.get('suit'),'\n')
输出:

value:  2
suit:  HEARTS

value:  KING
suit:  CLUBS

value:  QUEEN
suit:  HEARTS

这是一份字典清单。您只需遍历它并提取所需的元素

data ={
"success": True,
"deck_id": "jj1qy9tz9fkj",
"cards": [
    {
        "code": "2H",
        "image": "https://deckofcardsapi.com/static/img/2H.png",
        "images": {
            "svg": "https://deckofcardsapi.com/static/img/2H.svg",
            "png": "https://deckofcardsapi.com/static/img/2H.png"
        },
        "value": "2",
        "suit": "HEARTS"
    },
    {
        "code": "KC",
        "image": "https://deckofcardsapi.com/static/img/KC.png",
        "images": {
            "svg": "https://deckofcardsapi.com/static/img/KC.svg",
            "png": "https://deckofcardsapi.com/static/img/KC.png"
        },
        "value": "KING",
        "suit": "CLUBS"
    },
    {
        "code": "QH",
        "image": "https://deckofcardsapi.com/static/img/QH.png",
        "images": {
            "svg": "https://deckofcardsapi.com/static/img/QH.svg",
            "png": "https://deckofcardsapi.com/static/img/QH.png"
        },
        "value": "QUEEN",
        "suit": "HEARTS"
    }
],
"remaining": 49
}

# iterate through your list
for el in data.get('cards'):
    print('value: ', el.get('value'))
    print('suit: ', el.get('suit'),'\n')
输出:

value:  2
suit:  HEARTS

value:  KING
suit:  CLUBS

value:  QUEEN
suit:  HEARTS

最短方法

cards=response\u数据['cards']
[为卡片中的卡片打印{card['suit']}的{card['value']}]
打印(f“还有{响应_数据['resisting']}卡剩余。”)

使用python,我们可以以更简洁的方式完成此任务。

最短方法

cards=response\u数据['cards']
[为卡片中的卡片打印{card['suit']}的{card['value']}]
打印(f“还有{响应_数据['resisting']}卡剩余。”)

使用python,我们可以以更简洁的方式完成这项工作。

这解决了我的问题。谢谢你的帮助。这解决了我的问题。谢谢你的帮助。