简体   繁体   中英

How do I filter a json print to only spit out certain values

Code:

response = requests.request("GET", url, headers=headers, params=querystring)
print(json.dumps(response.json(), indent=4))

Output:

{
    "errors": [],
    "result": [
        {
            "hex": "18:93:d7",
            "b16": "1893d7",
            "name": "texas instruments",
            "address": "12500 ti blvd dallas tx 75243 us"
        }
    ]
}

My Goal:

I want to only print hex, name & address. i want to prettify it aswell but before i do that i need to filter the output

for i in 'hex', 'name', 'address':
    print(i.capitalize(), response.json()['result'][0][i])

and you'll get:

Hex 18:93:d7
Name texas instruments
Address 12500 ti blvd dallas tx 75243 us

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM