import codecs


def output(counter, top_n=None, output_counts=True):
    """Prints a counter with optional formatting
    :param counter: A Counter object to print
    :param top_n: The top N most common terms to print (default print all)
    :param output_counts: If true, prints term followed by count, separated by tab
    """
    if output_counts:
        def printer(n):
            return codecs.encode(u'{term}\t{count}'.format(term=n[0], count=n[1]), 'utf-8')
    else:
        def printer(n):
            return codecs.encode(n[0], 'utf-8')
    for n in counter.most_common(top_n):
        print(printer(n))