(curs, desc, fields = [])
Display multirow query as a table.
Display multirow query as a table.
Display multirow query as a table .
def display_result(curs, desc, fields = []):
    """Display multirow query as a table."""

    rows = curs.fetchall()

    if not fields:
        fields = [f[0] for f in curs.description]

    widths = [10] * len(fields)
    for i, f in enumerate(fields):
        rlen = len(f)
        if rlen > widths[i]:
            widths[i] = rlen
    for row in rows:
        for i, k in enumerate(fields):
            rlen = row[k] and len(str(row[k])) or 0
            if rlen > widths[i]:
                widths[i] = rlen
    widths = [w + 2 for w in widths]

    fmt = '%%-%ds' * (len(widths) - 1) + '%%s'
    fmt = fmt % tuple(widths[:-1])
    if desc:
        print(desc)
    print(fmt % tuple(fields))
    print(fmt % tuple([ '-' * (w - 2) for w in widths ]))

    for row in rows:
        print(fmt % tuple([row[k] for k in fields]))
    print('')
def display_result ( curs , desc , fields = [ ] ) : rows = curs . fetchall ( ) if not fields : fields = [ f [ 0 ] for f in curs . description ] widths = [ 10 ] * len ( fields ) for i , f in enumerate ( fields ) : rlen = len ( f ) if rlen > widths [ i ] : widths [ i ] = rlen for row in rows : for i , k in enumerate ( fields ) : rlen = row [ k ] and len ( str ( row [ k ] ) ) or 0 if rlen > widths [ i ] : widths [ i ] = rlen widths = [ w + 2 for w in widths ] fmt = '%%-%ds' * ( len ( widths ) - 1 ) + '%%s' fmt = fmt % tuple ( widths [ : - 1 ] ) if desc : print ( desc ) print ( fmt % tuple ( fields ) ) print ( fmt % tuple ( [ '-' * ( w - 2 ) for w in widths ] ) ) for row in rows : print ( fmt % tuple ( [ row [ k ] for k in fields ] ) ) print ( '' )