Parse the k-point and then continue to iterate over the band energies and occupations

def _parse_kpoint(line, lines):
    """Parse the k-point and then continue to iterate over the band energies and occupations"""
    toks = line.split()
    kpoint = [float(x) for x in toks[:3]]
    weight = float(toks[-1])
    newline = next(lines)

    bands_up = []
    occ_up = []
    bands_down = []
    occ_down = []
    ispin = None

    while len(newline.split()) > 0:
        toks = newline.split()
        if ispin is None:
            # there are two spins if there are 5 columns (two spin energies and occupancies) or
            # very probably if there are 3 columns and the last column's first value isn't 1.0
            # (which would be the occupancy of the lowest energy level)
            ispin = (len(toks) == 5) or (len(toks) == 3 and abs(float(toks[2]) - 1.0) > 1.0e-4)

        if len(toks) == 2:
            bands_up.append(float(toks[1]))
        elif len(toks) == 3 and not ispin:
            bands_up.append(float(toks[1]))
            occ_up.append(float(toks[2]))
        elif len(toks) == 3 and ispin:
            bands_up.append(float(toks[1]))
            bands_down.append(float(toks[2]))
        elif len(toks) == 5 and ispin:
            bands_up.append(float(toks[1]))
            bands_down.append(float(toks[2]))
            occ_up.append(float(toks[3]))
            occ_down.append(float(toks[4]))
        else:
            raise ValueError("Encountered {} when parsing k-point".format(newline))
        newline = next(lines)

    res = {"kpoint": kpoint, "weight": weight}
    if len(bands_down) > 0:
        res["energies"] = list(zip(bands_up, bands_down))
    else:
        res["energies"] = list(zip(bands_up))

    if len(occ_down) > 0:
        res["occupancies"] = list(zip(occ_up, occ_down))
    elif len(occ_up) > 0:
        res["occupancies"] = list(zip(occ_up))
      
    return res