def order_by_header(table, headers):
    ordered_table = []
    for row in table:
        # Tricky list comprehension got tricky when needing special handling
        # Lets do this the simplest way we can:
        row = {k:v for k,v in row.items() if k in headers}
        for h in headers:
            if h not in row:
                row[h] = ''
        ordered_row = OrderedDict(sorted(row.items(),
                                         key=lambda x:headers.index(x[0])))
        ordered_table.append(ordered_row)
    return ordered_table
Convert a list of dicts to a list or OrderedDicts ordered by headers
dataset/ETHPy150Open mosesschwartz/scrypture/scrypture/scrypture.py/order_by_header