A merry & shrewd investing community |
Best Of MI | Best Of | Favourites & Replies | All Boards | Post of the Week! ¤ |
![]() Best Of MI | Best Of | Favourites & Replies | All Boards | Post of the Week! ¤ |
Unthreaded | Threaded | Whole Thread (45) | Prev | Next |
import requests
def import_filtered_cpi():
# URL for BLS CPI data
url = "https://download.bls.gov/pub/time.series/cu/cu.dat..."
# Set User-Agent to avoid 403 errors
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
}
# Fetch data
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise exception for non-200 status codes
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
return
# Split data into rows
data = response.text
rows = data.strip().split("\n")
# Map period to month (M01 -> 01, M02 -> 02, etc.)
period_to_month = {
"M01": "01", "M02": "02", "M03": "03", "M04": "04",
"M05": "05", "M06": "06", "M07": "07", "M08": "08",
"M09": "09", "M10": "10", "M11": "11", "M12": "12"
}
# Prepare output file
output_file = "cpi_filtered_data.txt"
# Filter rows and write to file
with open(output_file, "w", encoding="utf-8") as f:
# Write headers
f.write("date\tvalue\n")
# Filter for series_id = "CUUR0000SA0" and period != "M13"
filtered_rows = []
for row in rows[1:]: # Skip header
columns = row.split("\t")
series_id = columns[0].strip()
year = columns[1].strip()
period = columns[2].strip()
value = columns[3].strip()
if series_id == "CUUR0000SA0" and period != "M13":
# Convert period to month and format date as YYYY-MM-01
if period in period_to_month:
date = f"{year}-{period_to_month[period]}-01"
filtered_rows.append([date, value])
f.write(f"{date}\t{value}\n")
# Print summary
print(f"Filtered {len(filtered_rows)} rows written to {output_file}")
if __name__ == "__main__":
import_filtered_cpi()
Unthreaded | Threaded | Whole Thread (45) | Prev | Next |