from datetime import datetime


def parse_iso_8601(dt_str):
    """
    Parse an ISO 8601 date string into a datetime. Supports the following date
    styles:

    2017-01-01
    20170101
    2017 01 01
    """
    try:
        return datetime.strptime(dt_str, "%Y-%m-%d")
    except ValueError:
        pass
    try:
        return datetime.strptime(dt_str, "%Y%m%d")
    except ValueError:
        pass
    return datetime.strptime(dt_str, "%Y %m %d")