Python prettytable美化你的输出
发布日期:2021-05-14 21:18:21 浏览次数:19 分类:精选文章

本文共 7027 字,大约阅读时间需要 23 分钟。

看代码:

#!/usr/bin/python# -*- coding:utf-8 -*-import prettytable as ptfrom prettytable import from_csv from prettytable import from_db_cursor import MySQLdbimport osimport timedef creatept():    tb = pt.PrettyTable()    tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"]    tb.add_row(["Adelaide",1295, 1158259, 600.5])    tb.add_row(["Brisbane",5905, 1857594, 1146.4])    tb.add_row(["Darwin", 112, 120900, 1714.7])    tb.add_row(["Hobart", 1357, 205556,619.5])    print(tb)    return tbdef ptfromcvs():    fp = open('/home/hp/Desktop/James_Harden.csv', "r")     pt = from_csv(fp)     fp.close()    print(pt)def ptfromsql():    db = MySQLdb.connect("127.0.0.1", "root",132456","launcher", charset='utf8')    cursor = db.cursor()    sql="SELECT mac,sn,device_id FROM launcher.iflyos where iflyos_devid='' limit 10"    cursor.execute(sql)    pt = from_db_cursor(cursor)    print(pt)def pytohtml(tb):    s = tb.get_html_string()    print(s)def writeLog(tb):    with open ("prettytable.log","a+") as f:        f.write(tb+"\n")if __name__ == '__main__':    tb=creatept()    pytohtml(tb)    ptfromsql()    ptfromcvs()    writeLog(str(tb))

输出:

+-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------++-----------+------+------------+-----------------+| City name | Area | Population | Annual Rainfall |+-----------+------+------------+-----------------+|  Adelaide | 1295 |  1158259   |      600.5      ||  Brisbane | 5905 |  1857594   |      1146.4     ||   Darwin  | 112  |   120900   |      1714.7     ||   Hobart  | 1357 |   205556   |      619.5      |+-----------+------+------------+-----------------+
+--------------+------------------+----------------------------------+|     mac      |        sn        |            device_id             |+--------------+------------------+----------------------------------+| BC67D48B9918 | PTNEHOPZ9947D73D | 7a43b5341c3e722a7fe1afc46351aaf7 || BC5AA084386E | PTM38R296Q2A8DKZ | 94e36ed37688780a245b13354bb83a4b || BC33C01F9F68 | PTGGDK3ZT4ASFX40 | 0242811be3d242c3b60198d2f5b1fbdc || BC0185ABEE19 | PTXF7XZQOXMV872N | 4ac5399adddac3285dcfc75ae9d09847 || BCFFEA44BB4F | PTXEYBG113XKDGRA | bc3beb670e138aeec77f7df95d0f9ce3 || BC96E14BFBEF | PTCQOKVADLS9NLQZ | 52b97117f1e7006c471a615481e8650b || BCE9222CE9F1 | PT98USHFTSXLFE88 | 048b60f91442922d687405f081549710 || BC27740B0B6C | PTL3PGGCL5EC3SHZ | 63a7345bfaee5445ea6a53d9d38f39fc || BC46775B92CB | PTHLXBGV0AT2AL39 | ede891b1897b6090ed49cc0fa49bc6d2 || BCBE3ACCB1E1 | PT8DAN3Z56A75BUG | 49d5871f0a3ea9b8c230b4af6c268ae2 |+--------------+------------------+----------------------------------++--------+------+--------+------+--------+------------+-----------+------+------+------+|  对手  | 胜负 | 主客场 | 命中 | 投篮数 | 投篮命中率 | 3分命中率 | 篮板 | 助攻 | 得分 |+--------+------+--------+------+--------+------------+-----------+------+------+------+|  勇士  |  胜  |   客   |  10  |   23   |   0.435    |   0.444   |  6   |  11  |  27  ||  国王  |  胜  |   客   |  8   |   21   |   0.381    |   0.286   |  3   |  9   |  27  ||  小牛  |  胜  |   主   |  10  |   19   |   0.526    |   0.462   |  3   |  7   |  29  ||  灰熊  |  负  |   主   |  8   |   20   |    0.4     |    0.25   |  5   |  8   |  22  ||  76人  |  胜  |   客   |  10  |   20   |    0.5     |    0.25   |  3   |  13  |  27  ||  黄蜂  |  胜  |   客   |  8   |   18   |   0.444    |    0.4    |  10  |  11  |  27  ||  灰熊  |  负  |   客   |  6   |   19   |   0.316    |   0.222   |  4   |  8   |  20  ||  76人  |  负  |   主   |  8   |   21   |   0.381    |   0.429   |  4   |  7   |  29  || 尼克斯 |  胜  |   客   |  9   |   23   |   0.391    |   0.353   |  5   |  9   |  31  ||  老鹰  |  胜  |   客   |  8   |   15   |   0.533    |   0.545   |  3   |  11  |  29  ||  爵士  |  胜  |   主   |  19  |   25   |    0.76    |   0.875   |  2   |  13  |  56  ||  骑士  |  胜  |   主   |  8   |   21   |   0.381    |   0.429   |  11  |  13  |  35  ||  灰熊  |  胜  |   主   |  11  |   25   |    0.44    |   0.429   |  4   |  8   |  38  || 步行者 |  胜  |   客   |  9   |   21   |   0.429    |    0.25   |  5   |  15  |  26  ||  猛龙  |  负  |   主   |  8   |   25   |    0.32    |   0.273   |  6   |  11  |  38  ||  太阳  |  胜  |   客   |  12  |   22   |   0.545    |   0.545   |  2   |  7   |  48  ||  灰熊  |  胜  |   客   |  9   |   20   |    0.45    |    0.5    |  5   |  7   |  29  ||  掘金  |  胜  |   主   |  6   |   16   |   0.375    |   0.143   |  8   |  9   |  21  || 尼克斯 |  胜  |   主   |  12  |   27   |   0.444    |   0.385   |  2   |  10  |  37  ||  篮网  |  胜  |   主   |  13  |   20   |    0.65    |   0.615   |  10  |  8   |  37  || 步行者 |  胜  |   主   |  8   |   22   |   0.364    |   0.333   |  8   |  10  |  29  ||  湖人  |  胜  |   客   |  13  |   22   |   0.591    |   0.444   |  4   |  9   |  36  ||  爵士  |  胜  |   客   |  8   |   19   |   0.421    |   0.333   |  5   |  3   |  29  || 开拓者 |  胜  |   客   |  16  |   29   |   0.552    |   0.571   |  8   |  3   |  48  ||  鹈鹕  |  胜  |   主   |  8   |   16   |    0.5     |    0.4    |  1   |  17  |  26  |+--------+------+--------+------+--------+------------+-----------+------+------+------+

这样比普通的输出看起来更明确更有逼格

上一篇:golang 连接redis
下一篇:python plt绘制统计图实例

发表评论

最新留言

不错!
[***.144.177.141]2025年04月15日 04时56分12秒