>> 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 |
+------------------+------------------+------------------+
Aquí está el código que utilizo para las tablas reStructuredText rápidos y sucios:
def make_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 = 1
for 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 = 0
return rst
def table_div(num_cols, col_width, header_flag):
if header_flag == 1:
return num_cols*('+' + (col_width)*'=') + '+\n'
else:
return num_cols*('+' + (col_width)*'-') + '+\n'
def normalize_cell(string, length):
return string + ((length - len(string)) * ' ')