签到成功

知道了

CNDBA社区CNDBA社区

Python模块 prettytable

2016-11-23 10:28 5005 0 原创 python开发
作者: Expect-乐

Python模块 prettytable

http://www.cndba.cn/Expect-le/article/391

1.prettytable说明

  Python通过prettytable模块可以将输出内容如表格方式整齐的输出。http://www.cndba.cn/Expect-le/article/391

2.安装prettytable

2.1查看系统是否已经安装prettytable模块

>>> import prettytable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named prettytable

2.2安装http://www.cndba.cn/Expect-le/article/391http://www.cndba.cn/Expect-le/article/391

#下载安装包http://www.cndba.cn/Expect-le/article/391

http://www.cndba.cn/Expect-le/article/391
http://www.cndba.cn/Expect-le/article/391

[root@ceph-osd1 XinFusion]# wget https://pypi.python.org/packages/source/P/PrettyTable/prettytable-0.7.2.tar.gz
--2016-11-22 21:19:54--  https://pypi.python.org/packages/source/P/PrettyTable/prettytable-0.7.2.tar.gz
Resolving pypi.python.org (pypi.python.org)... 151.101.16.223, 2a04:4e42:9::223
Connecting to pypi.python.org (pypi.python.org)|151.101.16.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 24784 (24K) [application/octet-stream]
Saving to: ‘prettytable-0.7.2.tar.gz’
100%[==========================================>] 24,784      54.0KB/s   in 0.4s   
2016-11-22 21:19:56 (54.0 KB/s) - ‘prettytable-0.7.2.tar.gz’ saved [24784/24784]

#解压

[root@ceph-osd1 XinFusion]# tar -zxvf prettytable-0.7.2.tar.gz
prettytable-0.7.2/
prettytable-0.7.2/prettytable_test.py
prettytable-0.7.2/README
prettytable-0.7.2/setup.py
prettytable-0.7.2/prettytable.py
prettytable-0.7.2/CHANGELOG
prettytable-0.7.2/setup.cfg
prettytable-0.7.2/PKG-INFO
prettytable-0.7.2/COPYING
prettytable-0.7.2/MANIFEST.in
prettytable-0.7.2/prettytable.egg-info/
prettytable-0.7.2/prettytable.egg-info/SOURCES.txt
prettytable-0.7.2/prettytable.egg-info/dependency_links.txt
prettytable-0.7.2/prettytable.egg-info/PKG-INFO
prettytable-0.7.2/prettytable.egg-info/top_level.txt

#编译

[root@ceph-osd1 XinFusion]# cd prettytable-0.7.2
[root@ceph-osd1 prettytable-0.7.2]# python setup.py build
running build
running build_py
creating build
creating build/lib
copying prettytable.py -> build/lib

#安装http://www.cndba.cn/Expect-le/article/391http://www.cndba.cn/Expect-le/article/391

[root@ceph-osd1 prettytable-0.7.2]# python setup.py install
running install
running bdist_egg
running egg_info
writing prettytable.egg-info/PKG-INFO
writing top-level names to prettytable.egg-info/top_level.txt
writing dependency_links to prettytable.egg-info/dependency_links.txt
reading manifest file 'prettytable.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'prettytable.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib/prettytable.py -> build/bdist.linux-x86_64/egg
byte-compiling build/bdist.linux-x86_64/egg/prettytable.py to prettytable.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying prettytable.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying prettytable.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying prettytable.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying prettytable.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/prettytable-0.7.2-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing prettytable-0.7.2-py2.7.egg
Copying prettytable-0.7.2-py2.7.egg to /usr/lib/python2.7/site-packages
Adding prettytable 0.7.2 to easy-install.pth file
Installed /usr/lib/python2.7/site-packages/prettytable-0.7.2-py2.7.egg
Processing dependencies for prettytable==0.7.2
Finished processing dependencies for prettytable==0.7.2

3.使用实例http://www.cndba.cn/Expect-le/article/391

>>> from prettytable import PrettyTable
>>> x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
>>> x.align["City name"] = "l"# Left align city names
>>> x.padding_width = 1# One space between column edges and contents (default)
>>> x.add_row(["Adelaide",1295, 1158259, 600.5])  
>>> x.add_row(["Brisbane",5905, 1857594, 1146.4]) 
>>> x.add_row(["Darwin", 112, 120900, 1714.7])
>>> x.add_row(["Hobart", 1357, 205556, 619.5])
>>> x.add_row(["Sydney", 2058, 4336374, 1214.8])
>>> x.add_row(["Expect-le", 1566, 3806092, 646.9])

结果:非常好看

>>> print x
+-----------+------+------------+-----------------+
| 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      |
| Sydney    | 2058 |  4336374   |      1214.8     |
| Expect-le | 1566 |  3806092   |      646.9      |
+-----------+------+------------+-----------------+


版权声明:本文为博主原创文章,未经博主允许不得转载。

Python prettytable

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
Expect-乐

Expect-乐

关注

Without the continuous bitter cold, there can be no fragrant plum blossom

  • 336
    原创
  • 6
    翻译
  • 100
    转载
  • 41
    评论
  • 访问:1567057次
  • 积分:1957
  • 等级:核心会员
  • 排名:第4名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ