Script that is in Javascript for Python

Asked

Viewed 65 times

-3

am with the following Javascript function:

function main() {
let data = fazGet("https://economia.awesomeapi.com.br/json/all");
let moedaJSON = JSON.parse(data);
console.log(moedaJSON)
const keys = Object.keys(moedaJSON)
keys.forEach(key => {
     document.getElementById("Nome1").innerHTML += moedaJSON[key]["name"] + "<br>";
     document.getElementById("Alta1").innerHTML += moedaJSON[key]["high"] + "<br>";
     document.getElementById("Baixa1").innerHTML += moedaJSON[key]["low"] + "<br>";
     document.getElementById("Compra1").innerHTML += moedaJSON[key]["bid"] + "<br>";
     document.getElementById("Venda1").innerHTML += moedaJSON[key]["ask"] + "<br>";
})

I want to do the same thing in Python, however, the variable "key" it has different names. It refers to an iten of an API dictionary that I will put right below.

moedas = {
"USD": {
    "code": "USD",
    "codein": "BRL",
    "name": "Dólar Americano/Real Brasileiro",
    "high": "5.6769",
    "low": "5.6142",
    "varBid": "0.0038",
    "pctChange": "0.07",
    "bid": "5.6191",
    "ask": "5.6208",
    "timestamp": "1618585377",
    "create_date": "2021-04-16 12:02:59"
},
"DOGE": {
    "code": "DOGE",
    "codein": "BRL",
    "name": "Dogecoin/Real Brasileiro",
    "high": "2.36",
    "low": "0.733879",
    "varBid": "1.43577199",
    "pctChange": "192.92",
    "bid": "2.18",
    "ask": "2.18",
    "timestamp": "1618584926",
    "create_date": "2021-04-16 11:55:27"
}}

NOTE: This dictionary above it is the same as this link, I only put a part of it as an example and not to be something big. Link here

In Json as you can see, we have "USD", "DOGE", etc... How do I make these names become variaves to facilitate the code. All this later I will put with Django so this need to change the script.

  • 1

    There is no way to convert the DOM HTML for python.

  • It doesn’t have to be the DOM, just the way to get the API value. I have a dictionary inside another dictionary, I want to call the values of the second dictionary without having to type the item of the first.

  • @Augustovasques is talking about DOM in the browser or is talking about DOM only?

  • @Guilhermenascimento, is the DOM HTML, as the link passed, because the MCVE of AP makes extensive use of this. So far I do not understand the question and much less the answer, both have no relation to the example presented.

  • From the link you sent "DOM was designed to be independent of any specific programming language" @Augustovasques, I’m having trouble understanding your first comment yet

  • @Guilhermenascimento, how would that line look document.getElementById("Nome1").innerHTML += moedaJSON[key]["name"] + "<br>"; python?

  • 1

    @Augustovasques this is not a matter of DOM, it is a specific feature of the DOM interface in Javascript, the problem is that the first comment meant something else.

  • @Guilhermenascimento, what is the question Script that is in Javascript for Python. I can not do because the DOM used by the function prevents me translation.

  • @Augustovasques I am not saying that the question is not confused, I just want to say that the first comment sounds something else, because it was not "gift" used (DOM is the "model"), but the "API DOM" that there is not something exactly the same, although it can be compensated in other ways, but now by your last comment it has come to understand that you refer to API of this and not to DOM in fact.

  • @Guilhermenascimento, correct. I understood what my failure was as to the terminology chosen and used. Thank you for the correction, I will strive to choose the terminology better and not be ambiguous when commenting.

Show 5 more comments

1 answer

1


I don’t know if I understand correctly but your question is how to get to the most internal values of the dictionary ?

If that’s it, it’s the code below, I do a go over the variable that brings the key and value and an internal go over the value that is also a dictionary, see that I didn’t need to specify the dictionary key name.

import requests

moedas = {
"USD": {
    "code": "USD",
    "codein": "BRL",
    "name": "Dólar Americano/Real Brasileiro",
    "high": "5.6769",
    "low": "5.6142",
    "varBid": "0.0038",
    "pctChange": "0.07",
    "bid": "5.6191",
    "ask": "5.6208",
    "timestamp": "1618585377",
    "create_date": "2021-04-16 12:02:59"
},
"DOGE": {
    "code": "DOGE",
    "codein": "BRL",
    "name": "Dogecoin/Real Brasileiro",
    "high": "2.36",
    "low": "0.733879",
    "varBid": "1.43577199",
    "pctChange": "192.92",
    "bid": "2.18",
    "ask": "2.18",
    "timestamp": "1618584926",
    "create_date": "2021-04-16 11:55:27"
}}

#Lendo de uma página
r = requests.get('https://economia.awesomeapi.com.br/json/all')
#Pegar de uma Página http
#moedas = r.json()

for chaveMaster, valorMaster in moedas.items():
    print(f'Chaves Master: {chaveMaster}')
    for chave, valor in valorMaster.items():
        print(f'   Chave: {chave} - Valor: {valor}')

inserir a descrição da imagem aqui

  • Thank you very much!

  • opa, you’re welcome...

Browser other questions tagged

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