Skip to content

KeyError when attempting to access Parser #17

@cjwinchester

Description

@cjwinchester

(moved from #13)

Looking at precinct-level results from the 2014 and 2016 primaries in Sarpy County, Nebraska -- in both cases, I'm getting a vote type KeyError when I try to access the Parser instance. I'm running Python 3.4.

The XML file for the 2014 primary is here.

Here's the code I'm stepping through for the 2014 results, up to the point where it breaks:

from __future__ import print_function
from zipfile import ZipFile

try:
    from StringIO import StringIO as ioDuder
except ImportError:
    from io import BytesIO as ioDuder

import unicodecsv
import requests
import clarify


county = 'Sarpy'

url = 'http://results.enr.clarityelections.com/NE/Sarpy/51545/184335/en/summary.html'

election_type = 'primary'

def clarify_sarpy():
    '''
    1. Fetch zipped XML file.
    2. Unzip in memory.
    3. Load into Clarify.
    4. Loop over results, write to file.
    '''

    # discover path to zipfile and fetch
    s = clarify.Jurisdiction(url=url, level='county')
    r = requests.get(s.report_url('xml'), stream=True)
    z = ZipFile(ioDuder(r.content))

    # hand off to clarify
    p = clarify.Parser()
    p.parse(z.open('detail.xml'))

    for result in p.results:
        print(result)

Here's the traceback:

Traceback (most recent call last):
  File "parse_2014_primary_sarpy_precinct.py", line 132, in <module>
    clarify_sarpy()
  File "parse_2014_primary_sarpy_precinct.py", line 35, in clarify_sarpy
    p.parse(z.open('detail.xml'))
  File "/home/cjwinchester/.virtualenvs/clarify/local/lib/python3.4/site-packages/clarify/parser.py", line 48, in parse
    self._contests = self._parse_contests(tree, self._result_jurisdiction_lookup)
  File "/home/cjwinchester/.virtualenvs/clarify/local/lib/python3.4/site-packages/clarify/parser.py", line 256, in _parse_contests
    return [self._parse_contest(el, result_jurisdiction_lookup) for el in contest_els]
  File "/home/cjwinchester/.virtualenvs/clarify/local/lib/python3.4/site-packages/clarify/parser.py", line 256, in <listcomp>
    return [self._parse_contest(el, result_jurisdiction_lookup) for el in contest_els]
  File "/home/cjwinchester/.virtualenvs/clarify/local/lib/python3.4/site-packages/clarify/parser.py", line 283, in _parse_contest
    for r in self._parse_no_choice_results(contest_el, result_jurisdiction_lookup, contest):
  File "/home/cjwinchester/.virtualenvs/clarify/local/lib/python3.4/site-packages/clarify/parser.py", line 322, in _parse_no_choice_results
    subjurisdiction = result_jurisdiction_lookup[subjurisdiction_el.attrib['name']]
KeyError: 'ABSENTEE'

Thanks for taking a look!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions