Home > Blockchain >  How can i select particular currency from api in python?
How can i select particular currency from api in python?

Time:10-02

I want to select TRY currency from an api. When i print that request text, it shows dozens of currencies. How can i select only one currency (TRY)?

Code is below. This code gives the currency list.

import pandas as pd
from datetime import datetime
 
 
rsp = requests.get("https://openexchangerates.org/api/latest.json?app_id=c92e9bfec2584ff0848965f86681ec37").json()
 
print(rsp)

CodePudding user response:

You can either use square brackets or .get() to access a key of the result you got from the api (as a dictionary)

import requests
rsp = requests.get("https://openexchangerates.org/api/latest.json?app_id=c92e9bfec2584ff0848965f86681ec37").json()

In your case TRY is listed inside rates. You can access it like this:

rsp.get("rates").get("TRY")

CodePudding user response:

import pandas as pd
import requests
from datetime import datetime
 
 
rsp = requests.get("https://openexchangerates.org/api/latest.json?app_id=c92e9bfec2584ff0848965f86681ec37").json()
 
print(rsp["rates"]["TRY"])
  • Related