I can not understand what the error is.

from prettytable import PrettyTable x = PrettyTable() x.field_names = [] x.field_names.append("example") x.add_row(["Example"]) print(x) KeyError: 'example' 

After calling x, an error "KeyError" occurs, and it occurs only when I add elements to the x.field_names list using the methods append, insert, etc., while manually adding any problems.

    1 answer 1

    You use the module incorrectly - there is a special method .add_column(...) for adding columns:

     In [24]: x = PrettyTable() In [25]: x.add_column("col1", [1,2,3]) In [26]: x.add_column("col2", [3,2,1]) In [27]: print(x) +------+------+ | col1 | col2 | +------+------+ | 1 | 3 | | 2 | 2 | | 3 | 1 | +------+------+ 

    or specify all columns in the constructor:

     In [36]: x = PrettyTable(['col1','col2']) In [37]: x.add_row(['a','b']) In [38]: x.add_row(['c','e']) In [39]: print(x) +------+------+ | col1 | col2 | +------+------+ | a | b | | c | e | +------+------+