co-early-votes.py
def table_parser(table):
    '''
    given a table from a PDFplumber page -- a list of lists with every
    data point for a county and gender total in a single row -- melt
    into a tidy list of dictionaries with keys that match `OUTCOLS` above
    '''

    # list to dump data into
    outlist = []

    # placeholder value for county, which will be updated as we iterate
    county = None

    # loop over the rows in the table
    for row in table:

        # skip the headers
        if 'COUNTY' in row[0]:
            continue

        # if there is a value in the first cell, it's the name
        # of the county, so we need to update the placeholder value
        if row[0]:
            county = row[0]

        # gender is in the second cell
        gender = row[1]

        # now we can chop up the row -- which lists totals for this county and
        # this gender for every party -- into several lists, one for each
        # party

        # first, loop over the list of columns in the PDF data
        for i, col in enumerate(INCOLS):

            # skip county and gender
            if col.upper() in ['COUNTY', 'GENDER']:
                continue
            else:

                # the name of the party is wherever we're at in the list of
                # PDF columns -- 'ACN', 'APV', etc.
                party = col

                # using the index value (`i`) for the PDF columns we're
                # iterating over, grab the associated value out of the
                # `row` of data we're working on ... while we're at it,
                # kill out commas and coerce to integer
                count = int(row[i].replace(',', ''))

                # turn the whole thing into a dictionary by marrying
                # the list of data to the CSV columns defined in the
                # `OUTCOLS` variable above
                record = dict(zip(OUTCOLS, [report_date, county, gender,
                                            party, count]))

                # drop it into the outlist
                outlist.append(record)

    # return the data list
    return outlist

    given a table from a PDFplumber page -- a list of lists with every
    data point for a county and gender total in a single row -- melt
    into a tidy list of dictionaries with keys that match `OUTCOLS` above
    
given a table from a PDFplumber page -- a list of lists with every
data point for a county and gender total in a single row -- melt
into a tidy list of dictionaries with keys that match `OUTCOLS` above
given a table from a PDFplumber page -- a list of lists with every data point for a county and gender total in a single row -- melt into a tidy list of dictionaries with keys that match ` OUTCOLS ` above
def table_parser(table):
    outlist = []
    county = None
    for row in table:
        if 'COUNTY' in row[0]:
            continue
        if row[0]:
            county = row[0]
        gender = row[1]
        for i, col in enumerate(INCOLS):
            if col.upper() in ['COUNTY', 'GENDER']:
                continue
            else:
                party = col
                count = int(row[i].replace(',', ''))
                record = dict(zip(OUTCOLS, [report_date, county, gender,
                                            party, count]))
                outlist.append(record)
    return outlist
def table_parser ( table ) : outlist = [ ] county = None for row in table : if 'COUNTY' in row [ 0 ] : continue if row [ 0 ] : county = row [ 0 ] gender = row [ 1 ] for i , col in enumerate ( INCOLS ) : if col . upper ( ) in [ 'COUNTY' , 'GENDER' ] : continue else : party = col count = int ( row [ i ] . replace ( ',' , '' ) ) record = dict ( zip ( OUTCOLS , [ report_date , county , gender , party , count ] ) ) outlist . append ( record ) return outlist
given a table from a PDFplumber page -- a list of lists with every
data point for a county and gender total in a single row -- melt
into a tidy list of dictionaries with keys that match `OUTCOLS` above
given a table from a PDFplumber page -- a list of lists with every data point for a county and gender total in a single row -- melt into a tidy list of dictionaries with keys that match ` OUTCOLS ` above
'''
    given a table from a PDFplumber page -- a list of lists with every
    data point for a county and gender total in a single row -- melt
    into a tidy list of dictionaries with keys that match `OUTCOLS` above
    ''' # list to dump data into # placeholder value for county, which will be updated as we iterate # loop over the rows in the table # skip the headers # if there is a value in the first cell, it's the name # of the county, so we need to update the placeholder value # gender is in the second cell # now we can chop up the row -- which lists totals for this county and # this gender for every party -- into several lists, one for each # party # first, loop over the list of columns in the PDF data # skip county and gender # the name of the party is wherever we're at in the list of # PDF columns -- 'ACN', 'APV', etc. # using the index value (`i`) for the PDF columns we're # iterating over, grab the associated value out of the # `row` of data we're working on ... while we're at it, # kill out commas and coerce to integer # turn the whole thing into a dictionary by marrying # the list of data to the CSV columns defined in the # `OUTCOLS` variable above # drop it into the outlist # return the data list
table