cuducos/calculadora-do-cidadao

Criar versão independente do BACEN

cuducos opened this issue · 0 comments

Exemplo com pip install rows[xls] para IPCA:

from urllib.request import urlretrieve
from tempfile import TemporaryDirectory
from zipfile import ZipFile

from rows.plugins.xls import import_from_xls


MONTHS = ("JAN", "FEV", "MAR", "ABR", "MAI", "JUN", "JUL", "AGO", "SET", "OUT", "NOV", "DEZ")
URL = "ftp://ftp.ibge.gov.br/Precos_Indices_de_Precos_ao_Consumidor/IPCA/Serie_Historica/ipca_SerieHist.zip"


def ipca():
    # download & unarchive & load
    filename, _ = urlretrieve(URL)
    with ZipFile(filename) as archive, TemporaryDirectory() as tmp:
        excel_file, *_ = archive.infolist()
        path = archive.extract(excel_file, path=tmp)
        table = import_from_xls(path, end_column=2)
        
    # read & deserialize
    last_year = None
    for year, month, value in table:
        if month not in MONTHS:
            continue

        year = int(year or last_year)
        month = MONTHS.index(month) + 1
        value = float(value)

        yield year, month, value
        last_year = year


for row in ipca():
    print(row)

Saída:

(1994, 1, 141.31)
(1994, 2, 198.22)
(1994, 3, 282.96)
(1994, 4, 403.73)
(1994, 5, 581.49)
(1994, 6, 857.29)
(1994, 7, 915.93)
(1994, 8, 932.97)
(1994, 9, 947.24)
(1994, 10, 972.06)
(1994, 11, 999.37)
(1994, 12, 1016.46)
(1995, 1, 1033.74)
(1995, 2, 1044.28)
(1995, 3, 1060.47)
(1995, 4, 1086.24)
(1995, 5, 1115.24)
(1995, 6, 1140.44)
(1995, 7, 1167.35)
(1995, 8, 1178.91)
(1995, 9, 1190.58)
(1995, 10, 1207.37)
(1995, 11, 1225.12)
(1995, 12, 1244.23)
(1996, 1, 1260.9)
(1996, 2, 1273.89)
(1996, 3, 1278.35)
(1996, 4, 1294.46)
(1996, 5, 1310.25)
(1996, 6, 1325.84)
(1996, 7, 1340.56)
(1996, 8, 1346.46)
(1996, 9, 1348.48)
(1996, 10, 1352.53)
(1996, 11, 1356.86)
(1996, 12, 1363.24)
(1997, 1, 1379.33)
(1997, 2, 1386.23)
(1997, 3, 1393.3)
(1997, 4, 1405.56)
(1997, 5, 1411.32)
(1997, 6, 1418.94)
(1997, 7, 1422.06)
(1997, 8, 1421.78)
(1997, 9, 1422.63)
(1997, 10, 1425.9)
(1997, 11, 1428.32)
(1997, 12, 1434.46)
(1998, 1, 1444.64)
(1998, 2, 1451.29)
(1998, 3, 1456.22)
(1998, 4, 1459.71)
(1998, 5, 1467.01)
(1998, 6, 1467.3)
(1998, 7, 1465.54)
(1998, 8, 1458.07)
(1998, 9, 1454.86)
(1998, 10, 1455.15)
(1998, 11, 1453.4)
(1998, 12, 1458.2)
(1999, 1, 1468.41)
(1999, 2, 1483.83)
(1999, 3, 1500.15)
(1999, 4, 1508.55)
(1999, 5, 1513.08)
(1999, 6, 1515.95)
(1999, 7, 1532.47)
(1999, 8, 1541.05)
(1999, 9, 1545.83)
(1999, 10, 1564.23)
(1999, 11, 1579.09)
(1999, 12, 1588.56)
(2000, 1, 1598.41)
(2000, 2, 1600.49)
(2000, 3, 1604.01)
(2000, 4, 1610.75)
(2000, 5, 1610.91)
(2000, 6, 1614.62)
(2000, 7, 1640.62)
(2000, 8, 1662.11)
(2000, 9, 1665.93)
(2000, 10, 1668.26)
(2000, 11, 1673.6)
(2000, 12, 1683.47)
(2001, 1, 1693.07)
(2001, 2, 1700.86)
(2001, 3, 1707.32)
(2001, 4, 1717.22)
(2001, 5, 1724.26)
(2001, 6, 1733.23)
(2001, 7, 1756.28)
(2001, 8, 1768.57)
(2001, 9, 1773.52)
(2001, 10, 1788.24)
(2001, 11, 1800.94)
(2001, 12, 1812.65)
(2002, 1, 1822.08)
(2002, 2, 1828.64)
(2002, 3, 1839.61)
(2002, 4, 1854.33)
(2002, 5, 1858.22)
(2002, 6, 1866.02)
(2002, 7, 1888.23)
(2002, 8, 1900.5)
(2002, 9, 1914.18)
(2002, 10, 1939.26)
(2002, 11, 1997.83)
(2002, 12, 2039.78)
(2003, 1, 2085.68)
(2003, 2, 2118.43)
(2003, 3, 2144.49)
(2003, 4, 2165.29)
(2003, 5, 2178.5)
(2003, 6, 2175.23)
(2003, 7, 2179.58)
(2003, 8, 2186.99)
(2003, 9, 2204.05)
(2003, 10, 2210.44)
(2003, 11, 2217.96)
(2003, 12, 2229.49)
(2004, 1, 2246.43)
(2004, 2, 2260.13)
(2004, 3, 2270.75)
(2004, 4, 2279.15)
(2004, 5, 2290.77)
(2004, 6, 2307.03)
(2004, 7, 2328.02)
(2004, 8, 2344.08)
(2004, 9, 2351.82)
(2004, 10, 2362.17)
(2004, 11, 2378.47)
(2004, 12, 2398.92)
(2005, 1, 2412.83)
(2005, 2, 2427.07)
(2005, 3, 2441.87)
(2005, 4, 2463.11)
(2005, 5, 2475.18)
(2005, 6, 2474.68)
(2005, 7, 2480.87)
(2005, 8, 2485.09)
(2005, 9, 2493.79)
(2005, 10, 2512.49)
(2005, 11, 2526.31)
(2005, 12, 2535.4)
(2006, 1, 2550.36)
(2006, 2, 2560.82)
(2006, 3, 2571.83)
(2006, 4, 2577.23)
(2006, 5, 2579.81)
(2006, 6, 2574.39)
(2006, 7, 2579.28)
(2006, 8, 2580.57)
(2006, 9, 2585.99)
(2006, 10, 2594.52)
(2006, 11, 2602.56)
(2006, 12, 2615.05)
(2007, 1, 2626.56)
(2007, 2, 2638.12)
(2007, 3, 2647.88)
(2007, 4, 2654.5)
(2007, 5, 2661.93)
(2007, 6, 2669.38)
(2007, 7, 2675.79)
(2007, 8, 2688.37)
(2007, 9, 2693.21)
(2007, 10, 2701.29)
(2007, 11, 2711.55)
(2007, 12, 2731.62)
(2008, 1, 2746.37)
(2008, 2, 2759.83)
(2008, 3, 2773.08)
(2008, 4, 2788.33)
(2008, 5, 2810.36)
(2008, 6, 2831.16)
(2008, 7, 2846.16)
(2008, 8, 2854.13)
(2008, 9, 2861.55)
(2008, 10, 2874.43)
(2008, 11, 2884.78)
(2008, 12, 2892.86)
(2009, 1, 2906.74)
(2009, 2, 2922.73)
(2009, 3, 2928.57)
(2009, 4, 2942.63)
(2009, 5, 2956.46)
(2009, 6, 2967.1)
(2009, 7, 2974.22)
(2009, 8, 2978.68)
(2009, 9, 2985.83)
(2009, 10, 2994.19)
(2009, 11, 3006.47)
(2009, 12, 3017.59)
(2010, 1, 3040.22)
(2010, 2, 3063.93)
(2010, 3, 3079.86)
(2010, 4, 3097.42)
(2010, 5, 3110.74)
(2010, 6, 3110.74)
(2010, 7, 3111.05)
(2010, 8, 3112.29)
(2010, 9, 3126.29)
(2010, 10, 3149.74)
(2010, 11, 3175.88)
(2010, 12, 3195.89)
(2011, 1, 3222.42)
(2011, 2, 3248.2)
(2011, 3, 3273.86)
(2011, 4, 3299.07)
(2011, 5, 3314.58)
(2011, 6, 3319.55)
(2011, 7, 3324.86)
(2011, 8, 3337.16)
(2011, 9, 3354.85)
(2011, 10, 3369.28)
(2011, 11, 3386.8)
(2011, 12, 3403.73)
(2012, 1, 3422.79)
(2012, 2, 3438.19)
(2012, 3, 3445.41)
(2012, 4, 3467.46)
(2012, 5, 3479.94)
(2012, 6, 3482.72)
(2012, 7, 3497.7)
(2012, 8, 3512.04)
(2012, 9, 3532.06)
(2012, 10, 3552.9)
(2012, 11, 3574.22)
(2012, 12, 3602.46)
(2013, 1, 3633.44)
(2013, 2, 3655.24)
(2013, 3, 3672.42)
(2013, 4, 3692.62)
(2013, 5, 3706.28)
(2013, 6, 3715.92)
(2013, 7, 3717.03)
(2013, 8, 3725.95)
(2013, 9, 3738.99)
(2013, 10, 3760.3)
(2013, 11, 3780.61)
(2013, 12, 3815.39)
(2014, 1, 3836.37)
(2014, 2, 3862.84)
(2014, 3, 3898.38)
(2014, 4, 3924.5)
(2014, 5, 3942.55)
(2014, 6, 3958.32)
(2014, 7, 3958.72)
(2014, 8, 3968.62)
(2014, 9, 3991.24)
(2014, 10, 4008.0)
(2014, 11, 4028.44)
(2014, 12, 4059.86)
(2015, 1, 4110.2)
(2015, 2, 4160.34)
(2015, 3, 4215.26)
(2015, 4, 4245.19)
(2015, 5, 4276.6)
(2015, 6, 4310.39)
(2015, 7, 4337.11)
(2015, 8, 4346.65)
(2015, 9, 4370.12)
(2015, 10, 4405.95)
(2015, 11, 4450.45)
(2015, 12, 4493.17)
(2016, 1, 4550.23)
(2016, 2, 4591.18)
(2016, 3, 4610.92)
(2016, 4, 4639.05)
(2016, 5, 4675.23)
(2016, 6, 4691.59)
(2016, 7, 4715.99)
(2016, 8, 4736.74)
(2016, 9, 4740.53)
(2016, 10, 4752.86)
(2016, 11, 4761.42)
(2016, 12, 4775.7)
(2017, 1, 4793.85)
(2017, 2, 4809.67)
(2017, 3, 4821.69)
(2017, 4, 4828.44)
(2017, 5, 4843.41)
(2017, 6, 4832.27)
(2017, 7, 4843.87)
(2017, 8, 4853.07)
(2017, 9, 4860.83)
(2017, 10, 4881.25)
(2017, 11, 4894.92)
(2017, 12, 4916.46)
(2018, 1, 4930.72)
(2018, 2, 4946.5)
(2018, 3, 4950.95)
(2018, 4, 4961.84)
(2018, 5, 4981.69)
(2018, 6, 5044.46)
(2018, 7, 5061.11)
(2018, 8, 5056.56)
(2018, 9, 5080.83)
(2018, 10, 5103.69)
(2018, 11, 5092.97)
(2018, 12, 5100.61)
(2019, 1, 5116.93)
(2019, 2, 5138.93)
(2019, 3, 5177.47)
(2019, 4, 5206.98)
(2019, 5, 5213.75)
(2019, 6, 5214.27)
(2019, 7, 5224.18)
(2019, 8, 5229.93)
(2019, 9, 5227.84)
(2019, 10, 5233.07)
(2019, 11, 5259.76)
(2019, 12, 5320.25)

Fonte: IPCA > Série histórica