def to_csv(objects, filename, digits=5, warnings=True):
    if not isinstance(objects, list):
        objects = [objects]
    data = [flatten(obj) for obj in objects]
    all_keys = [d for datum in data for d in datum.keys()]
    field_names = sorted(set(all_keys), key=lambda x: all_keys.index(x))
    with open(filename, 'w') as f:
        w = csv.writer(f)
        w.writerow(field_names)
        def make_repr(item):
            if item is None:
                return None
            elif isinstance(item, float):
                return repr(round(item, digits))
            else:
                return str(item)
        for row in data:
            row = dict((k, make_repr(v)) for k, v in row.items())
            w.writerow([make_repr(row.get(k, None)) for k in field_names])
    if warnings:
        print("Successfully exported {} object(s) to {}".format(len(objects),
              filename))