import codecs


def _get_encoding(enc, read=False):
    """Check if encoding exists else return utf-8."""

    try:
        codecs.lookup(enc)
    except LookupError:
        enc = 'utf-8'

    # If reading utf-8, lets just read with utf-8-sig
    # so we don't have to worry about an accidental BOM
    if read and enc.lower().replace('-', '') == 'utf8':
        enc = 'utf-8-sig'

    return enc