'Given a file-name fileName, csv will write the contents of
the Players sequence to fileName formatted as comma-separated values.
The resulting file can then be opened directly with programs like
Excel, Google Docs, Libre Office and Open Office.
Note that since each player in a Players sequence may have differing
statistical categories (like a quarterback and a receiver), the
minimum constraining set of statisical categories is used as the
header row for the resulting CSV file. This behavior can be changed
by setting \'allfields\' to True, which will use every available field
in the header.'

def csv(self, fileName, allfields=False):

    import csv
    (fields, rows) = (set([]), [])
    players = list(self)
    for p in players:
        for (field, stat) in p.stats.iteritems():
            fields.add(field)
    if allfields:
        for (statId, info) in statmap.idmap.iteritems():
            for field in info['fields']:
                fields.add(field)
    fields = sorted(list(fields))
    for p in players:
        d = {'name': p.name, 'id': p.playerid, 'home': ((p.home and 'yes') or 'no'), 'team': p.team, 'pos': 'N/A'}
        if (p.player is not None):
            d['pos'] = p.player.position
        for field in fields:
            if (field in p.__dict__):
                d[field] = p.__dict__[field]
            else:
                d[field] = ''
        rows.append(d)
    fieldNames = (['name', 'id', 'home', 'team', 'pos'] + fields)
    rows = ([dict(((f, f) for f in fieldNames))] + rows)
    csv.DictWriter(open(fileName, 'wb+'), fieldNames).writerows(rows)
