import csv
import collections


def csv_count(csvfile, column): #--------------------------------------------<<<
    """Generate a summary of unique values for a column/field.

    infile = a CSV file; must have a header row
    column = a column number or name

    Returns a dictionary whose keys are the unique values found in the
    specified field, and values are the count for each unique value.
    """
    colnames = open(csvfile, 'r').readline().strip().split(',')
    if isinstance(column, int):
        colno = column
        colname = colnames[colno]
    else:
        colname = column
        colno = 0 # default if not found in CSV header
        for fieldno, fieldname in enumerate(colnames):
            if fieldname.lower() == colname.lower():
                colno = fieldno
                break

    myreader = csv.reader(open(csvfile, 'r'), delimiter=',', quotechar='"')
    next(myreader, None) # skip header
    unique_values = collections.OrderedDict()
    for values in myreader:
        key = values[colno]
        if key in unique_values:
            unique_values[key] += 1
        else:
            unique_values[key] = 1

    return unique_values