tabulate is a module that allows you to display table data beautifully. It is not a part of standard Python library, so tabulate needs to be installed: pip install tabulate
Module supports such tabular data types as:
- list of lists (in general case - iterable of iterables)
- dictionary list (or any other iterable object with dictionaries). Keys are used as column names
- dictionary with iterable objects. Keys are used as column names
Function tabulate() is used to generate table:
from tabulate import tabulate
data = [["Original Sentence:", "Translated Sentence:"],
[self.word, str(translation.text)]]
return tabulate(data)
Parameter headers allows you to pass an additional argument that specifies column names:
data = [["Original Sentence:", "Translated Sentence:"],
[self.word, str(translation.text)]]
return tabulate(data, headers="firstrow" )
tabulate supports different table styles.
data = [["Original Sentence:", "Translated Sentence:"],
[self.word, str(translation.text)]]
return tabulate(data, headers="firstrow", tablefmt="fancy_grid" )