instant-data-scraper-utils/bin/fetch-census-examples.py
2024-07-16 18:08:36 -04:00

33 lines
939 B
Python
Executable file

#!/usr/bin/env python
import requests
import csv
import io
# Send a GET request to the US Census Bureau dataset URL
url = "https://www2.census.gov/programs-surveys/popest/datasets/2010-2019/cities/totals/sub-est2019_all.csv"
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Read the CSV content from the response
csv_content = io.StringIO(response.text)
# Create a CSV reader object
reader = csv.DictReader(csv_content)
# Fetch a small set of example data (e.g., first 5 rows)
example_data = []
for _ in range(5):
row = next(reader)
example_data.append(row)
# Print the example data for each column
for column_name in reader.fieldnames:
print(f"Column: {column_name}")
for row in example_data:
print(f" {row[column_name]}")
print()
else:
print("Failed to fetch the dataset.")