提问者:小点点

如何使用prettytable和mysql连接器从mysql中的数据在python中生成表?


我正在尝试制作一个类似excel的表,其中的行名和列名都是预定义的。 但我不能在prettytable中有列名。


共1个答案

匿名用户

您的问题有点含糊不清,但实际上您可以为表定义列名:

table = PrettyTable()

# define column names here
table.field_names = ['Heading One', 'Heading Two', 'Heading Three']

table.add_row(['Row One', 'Row One', 'Row One'])
table.add_row(['Row Two', 'Row Two', 'Row Two'])

print(table)

将输出:

也可以将SQL表直接转换为PrettyTable表。 我需要看到与DB交互的代码,以便在那里提供任何直接的帮助,但是一个好的开始应该在这里:http://zetcode.com/python/prettytable/