Python Api Data Filtering

Asked

Viewed 192 times

-2

I’m doing an algorithm on Python that makes a get in a url to dollar quotation, but the return of that get has a lot of unnecessary information I would like to know how to filter this data follows below the code and the response of the request.

import requests
import json

requisicao = requests.get('https://economia.awesomeapi.com.br/all/USD')
dicionario = json.loads(requisicao.text)
print(requisicao.content)

RESPONSE TO THE REQUEST

b'{"USD":{"code":"USD","codein":"BRL","name":"D\xc3\xb3lar Comercial","high":"5.3944","low":"5.3045","varBid":"0.0005","pctChange":"0.01","bid":"5.3237","ask":"5.3247","timestamp":"1606339799","create_date":"2020-11-25 19:00:01"}}'

1 answer

0

Here you make the code of a json:

dicionario = json.loads(requisicao.text)

You can filter through the Keys of the dictionary:

dicionario['USD']['name']
'Dólar Comercial'

dicionario['USD']['low']
'5.3242'
  • How do I implement this in the code?

  • It depends on what information you want to extract. The example I gave can be used for any key in your dictionary. The return of your request is a json with only the dollar, if it returns other currencies you could filter for example: dicionario['BRL']['low']

  • 1

    Thanks to be able to complete the algorithm, thanks bro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.