Skip to content Skip to sidebar Skip to footer

What Are Some Approaches To Outputting A Python Data Structure To Restructuredtext

I have a list of tuples in Python that I would like to output to a table in reStructuredText. The docutils library has great support for converting reStructuredText to other format

Solution 1:

Check out the tabulate package. It can output RST format by:

print tabulate(table, headers, tablefmt="rst")

Solution 2:

>> print make_table([['Name', 'Favorite Food', 'Favorite Subject'],
                     ['Joe', 'Hamburgers', 'Cars'],
                     ['Jill', 'Salads', 'American Idol'],
                     ['Sally', 'Tofu', 'Math']])

+------------------+------------------+------------------+| Name             | Favorite Food    | Favorite Subject |+==================+==================+==================+| Joe              | Hamburgers       | Cars             |+------------------+------------------+------------------+| Jill             | Salads           | American Idol    |+------------------+------------------+------------------+| Sally            | Tofu             | Math             |+------------------+------------------+------------------+

Here is the code I use for quick and dirty reStructuredText tables:

defmake_table(grid):
    cell_width = 2 + max(reduce(lambda x,y: x+y, [[len(item) for item in row] for row in grid], []))
    num_cols = len(grid[0])
    rst = table_div(num_cols, cell_width, 0)
    header_flag = 1for row in grid:
        rst = rst + '| ' + '| '.join([normalize_cell(x, cell_width-1) for x in row]) + '|\n'
        rst = rst + table_div(num_cols, cell_width, header_flag)
        header_flag = 0return rst

deftable_div(num_cols, col_width, header_flag):
    if header_flag == 1:
        return num_cols*('+' + (col_width)*'=') + '+\n'else:
        return num_cols*('+' + (col_width)*'-') + '+\n'defnormalize_cell(string, length):
    return string + ((length - len(string)) * ' ')

Solution 3:

I'm not aware of any libraries to output RST from python data structures, but it's pretty easy to format it yourself. Here's an example of formatting a list of python tuples to an RST table:

>>>data = [('hey', 'stuff', '3'),
            ('table', 'row', 'something'),
            ('xy', 'z', 'abc')]
>>>numcolumns = len(data[0])>>>colsizes = [max(len(r[i]) for r in data) for i inrange(numcolumns)]>>>formatter = ' '.join('{:<%d}' % c for c in colsizes)>>>rowsformatted = [formatter.format(*row) for row in data]>>>header = formatter.format(*['=' * c for c in colsizes])>>>output = header + '\n' + '\n'.join(rowsformatted) + '\n' + header>>>print output
===== ===== =========
hey   stuff 3        
table row   something
xy    z     abc      
===== ===== =========

Solution 4:

@cieplak's answer was great. I refined it a bit so that columns are sized independently

    print make_table( [      ['Name', 'Favorite Food', 'Favorite Subject'],
                             ['Joe', 'Hamburgrs', 'I like things with really long names'],
                             ['Jill', 'Salads', 'American Idol'],
                             ['Sally', 'Tofu', 'Math']])

    ===== ============= ==================================== 
    Name  Favorite Food FavoriteSubject===== ============= ==================================== 
    Joe   Hamburgrs     I like things with really long names 
    ----- ------------- ------------------------------------ 
    Jill  Salads        American Idol                        
    ----- ------------- ------------------------------------ 
    Sally TofuMath===== ============= ==================================== 

Here is the code

defmake_table(grid):
    max_cols = [max(out) for out inmap(list, zip(*[[len(item) for item in row] for row in grid]))]
    rst = table_div(max_cols, 1)

    for i, row inenumerate(grid):
        header_flag = Falseif i == 0or i == len(grid)-1: header_flag = True
        rst += normalize_row(row,max_cols)
        rst += table_div(max_cols, header_flag )
    return rst

deftable_div(max_cols, header_flag=1):
    out = ""if header_flag == 1:
        style = "="else:
        style = "-"for max_col in max_cols:
        out += max_col * style + " "

    out += "\n"return out


defnormalize_row(row, max_cols):
    r = ""for i, max_col inenumerate(max_cols):
        r += row[i] + (max_col  - len(row[i]) + 1) * " "return r + "\n"

Solution 5:

You can choose to dump into a CSV from Python and then use the csv-table feature of RST as in http://docutils.sourceforge.net/docs/ref/rst/directives.html#csv-table It has got a :file: directive to simply include a csv file with data.

Post a Comment for "What Are Some Approaches To Outputting A Python Data Structure To Restructuredtext"