import csv


def write_csv(f, extract, fields=None):
    """ Writers extract to file handle
    Parameters
    __________
    f : file handle (string mode)
    extract: list of dicts
    fields: list of str
        Field names to include in CSV.

    Returns
    _______
    f : file handle (string mode)
    """
    keys = fields if fields is not None else extract[0].keys()
    dict_writer = csv.DictWriter(f, keys, extrasaction='ignore')
    dict_writer.writeheader()
    dict_writer.writerows(extract)
    return f