It's worth browsing the legislator data file in a spreadsheet to get a feel for what it contains. I've embedded it in a Gist below:
filter.py
script with a parsing functionThis script uses the read_data()
method from fetch.py
to download the data – if it doesn't already exist – then provide it as raw text. The csv.DictReader
class is used to turn the lines of text into a list of dictionaries:
from fetch import read_data
from csv import DictReader
def parse_data():
"""
Returns a list of dictionaries from the raw text
based on what fetch.read_data() returns
"""
txt = read_data()
lines = txt.splitlines()
return list(DictReader(lines))
from fetch import read_data
from csv import DictReader
def parse_data():
"""
Returns a list of dictionaries from the raw text
based on what fetch.read_data() returns
"""
txt = read_data()
lines = txt.splitlines()
return list(DictReader(lines))
def get_birthday_rows_by_month_day(mth_day):
"""
Calls parse_data to get a list of dicts
Expects mth_day to be a string in this format:
MM-DD, e.g. "05-12", (May 12)
Returns a list of dicts that includes:
- dicts with 'in_office' == '1' (legislators still in office)
- dicts with the month and day components of 'birthdate'
equal to the mth_day string
"""
newrows = []
original_rows = parse_data()
for row in original_rows:
if row['in_office'] == '1':
rbdate = row['birthdate'][5:]
if rbdate == mth_day:
newrows.append(row)
# all done
return newrows