Python Json Parsing WildCards/Regex

Viewed 209

I am new to python and playing around with an exchange rate API.

I have been able to pull data and parse it successfully. The issue that I am having is with dates. What I am trying to do is pull down all currency rates using https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-09-01 and then split this out based on certain days.

I understand the structure of the data but I dont know how to parse anything passed [rates] as the date changes for each day.

Is there some way I can parse out the data for each specific day or even to ignore the day and just pull out all the ILS values from the json by something like ILS_val = parsed["rates"][SOMEWILDCARD]["ILS"] where SOMEWILDCARD will allow me skip all the dates and extract all the euro exchange rates

This is what I have so far

from urllib import *
import json

url = "https://api.exchangeratesapi.io/history?start_at=2018-01-01&end_at=2018-09-01&symbols=ILS,JPY"

thepage = urlopen(url).read().decode('utf-8')
parsed = json.loads(thepage)

rates = parsed["rates"]

file = open("currencyRates.txt", "w+")

for rate in rates.items():

    file.write(str(rate) + "\n")

file.close()
1 Answers

You can just iterate over your items:

# Getting the values: day[0] for dates & day[1] for values
rates = [day[1]['JPY'] for day in parsed["rates"].items()]

# Write into file
with open("currencyRates.txt", "w+") as file:
    [file.write(str(rate) + "\n") for rate in rates]
Related