get_string()方法是个不错的选择!print(pt.get_string(start=1,end=4))上面这句意思是获取表格从第二行开始到第五行的内容,并转化为字符串打印输出(当然,输出的肯定是一个表格了)。上面只是对行进行了切片(暂且这么叫吧)。那么,问题来了,如果我们只想获取姓名和性别这两列的内容,并打印输出,该如何...
from prettytable import PrettyTable def print_pretty(df0): df=df0.reset_index(inplace=False) columns=list(df.columns) table = PrettyTable(columns) rowNum = df.shape[0] for i in range(0,rowNum-1): table.add_row(list(df.iloc[i])) print(table.get_string(align="r")) SQL数据库表转pr...
fromprettytable import PrettyTable def print_pretty(df0): df=df0.reset_index(inplace=False) columns=list(df.columns) table =PrettyTable(columns) rowNum = df.shape[0] for i inrange(0,rowNum-1): table.add_row(list(df.iloc[i]))print(table.get_string(align="r")) SQL数据库表转prettytabl...
我们可以通过add_row方法在PrettyTable对象中添加行。
首先,我们需要安装它。pip install tabulate现在我们已准备好此模块,让我们通过简单示例了解在 python 中创建表格的过程。from tabulate import tabulatestudent_data = [["Li", "16"], ["Wang", "19"], ["Zhang", "21"], ["Zhou", "23"]]heading = ["Name", "Age"] # 表头print(tabul...
table.vrules=PrettyTable.PRETTY_TABLE_FRAME# 添加数据table.add_row(["Alice",25])table.add_row(["Bob",30])table.add_row(["Charlie",35])# 打印表格print(table) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. ...
defFinanceCalculator():'输入初始金额和月开销数,返回剩下的金额、当月的支出数、和最后的支出数'print'Enter opening balance:', RawBalance=float(raw_input()) Balance=RawBalanceprint'Enter monthly payment:', MonthlyPayment=float(raw_input())print'\t\tAmount\tRemaining'table= PrettyTable(['Pymt#',...
print(tabulate(student_data, headers=heading, tablefmt="pretty")) 输出: +---+---+ | Name | Age | +---+---+ | Li |16| | Wang |19| | Zhang |21| | Zhou |23| +---+---+ 在这里,传递一个新参数 tablefmt="pretty",为表格添加了边框线。tablefmt 还可以接受几个不同的选项,包...
prettyattempts to be close to the format emitted by the PrettyTables library: >>>print(tabulate(table, headers,tablefmt="pretty")) +---+---+ | item | qty | +---+---+ | spam | 42 | | eggs | 451 | | bacon | 0 |
def prettyPrint(self,trains): pt.title = 'Results for Query 浏览92提问于2018-08-07得票数 0 2回答 无法安装prettytable 、 % sudo pip -vvv install prettytable Getting page https://pypi.python.org/simple/prettytable/ Could not fetch URL https://pypi.python.org/simple/prettytable&# 浏览0提问...