Write the given table (list of dicts) to the given file as CSV.

    Writes UTF8-encoded, CSV-formatted text.

    ``f`` could be an opened file, sys.stdout, or a StringIO.

def _write_csv(f, table_):
    """Write the given table (list of dicts) to the given file as CSV.

    Writes UTF8-encoded, CSV-formatted text.

    ``f`` could be an opened file, sys.stdout, or a StringIO.

    """
    fieldnames = table_[0].keys()
    set_fieldname = set(table_[0].keys())
    # go through all the fields and find all the field names
    for row in table_:
        set_fieldname.update(set(row.keys()))

    # append the additonal fields sorted onto the end
    additional_fields = sorted(set_fieldname - set(table_[0].keys()))
    fieldnames += additional_fields

    writer = unicodecsv.DictWriter(f, fieldnames, encoding='utf-8')
    writer.writeheader()

    # Change lists into comma-separated strings.
    for dict_ in table_:
        for key, value in dict_.items():
            if type(value) in (list, tuple):
                dict_[key] = ', '.join([unicode(v) for v in value])

    writer.writerows(table_)