python之prettytable

来源:http://www.cnblogs.com/similarface/archive/2016/04/16/5397570.html
-Advertisement-
Play Games

PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables. It was inspire... ...


 

>>> from prettytable import PrettyTable
>>> x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])  
>>> x
<prettytable.PrettyTable object at 0x7f778da86ed0>
>>> print x
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+
>>> x.align['City name']
'c'
>>> x.align['City name']="1"
>>> print x
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+
>>> x.padding_width=1
>>> print(x)
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
+-----------+------+------------+-----------------+
>>> x.add_row(["Adelaide",1295, 1158259, 600.5])
>>> print(x)
+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
+-----------+------+------------+-----------------+
>>> 
#x.align=【'l','r','c'】left right center
#x.padding_width =[寬度]

>>> x = PrettyTable() 
>>> x.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"]) 
>>> x.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386]) 
>>> x.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092, 1554769]) 
>>> x.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9, 869.4])
>>> 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     |
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |
+-----------+------+------------+-----------------+

>>> os.system('cat ~/cj.csv')
語文,數學,英語
133,98,121
110,125,92
73,131,84
0
>>> from prettytable import from_csv 
>>> fp = open("cj.csv", "r")
>>> pt = from_csv(fp)
>>> fp.close()
>>> print pt
+------+------+------+
| 語文 | 數學 | 英語 |
+------+------+------+
| 133  |  98  | 121  |
| 110  | 125  |  92  |
|  73  | 131  |  84  |
+------+------+------+
>>> x.get_string(fields=["City name", "Population"])
u'+-----------------------------+------------------------------+\n|          City name          |          Population          |\n+-----------------------------+------------------------------+\n|           Adelaide          |           1158259            |\n|           Brisbane          |           1857594            |\n|            Darwin           |            120900            |\n|            Hobart           |            205556            |\n|            Sydney           |           4336374            |\n|          Melbourne          |           3806092            |\n|            Perth            |           1554769            |\n+-----------------------------+------------------------------+'
>>> 
>>> 
>>> 
>>> x.get_string(fields=["City name", "Population"])
u'+-----------------------------+------------------------------+\n|          City name          |          Population          |\n+-----------------------------+------------------------------+\n|           Adelaide          |           1158259            |\n|           Brisbane          |           1857594            |\n|            Darwin           |            120900            |\n|            Hobart           |            205556            |\n|            Sydney           |           4336374            |\n|          Melbourne          |           3806092            |\n|            Perth            |           1554769            |\n+-----------------------------+------------------------------+'
>>> print x.get_string(start=0,end=3)
+-----------------------------+------------------------+------------------------------+-----------------------------------+
|          City name          |          Area          |          Population          |          Annual Rainfall          |
+-----------------------------+------------------------+------------------------------+-----------------------------------+
|           Adelaide          |          1295          |           1158259            |               600.5               |
|           Brisbane          |          5905          |           1857594            |               1146.4              |
|            Darwin           |          112           |            120900            |               1714.7              |
+-----------------------------+------------------------+------------------------------+-----------------------------------+
>>> print x.get_string(sortby="Annual Rainfall")
+-----------------------------+------------------------+------------------------------+-----------------------------------+
|          City name          |          Area          |          Population          |          Annual Rainfall          |
+-----------------------------+------------------------+------------------------------+-----------------------------------+
|           Adelaide          |          1295          |           1158259            |               600.5               |
|            Hobart           |          1357          |            205556            |               619.5               |
|          Melbourne          |          1566          |           3806092            |               646.9               |
|            Perth            |          5386          |           1554769            |               869.4               |
|           Brisbane          |          5905          |           1857594            |               1146.4              |
|            Sydney           |          2058          |           4336374            |               1214.8              |
|            Darwin           |          112           |            120900            |               1714.7              |
+-----------------------------+------------------------+------------------------------+-----------------------------------+

>>> print x.get_string(sortby="Annual Rainfall", reversesort=True)
+-----------------------------+------------------------+------------------------------+-----------------------------------+
|          City name          |          Area          |          Population          |          Annual Rainfall          |
+-----------------------------+------------------------+------------------------------+-----------------------------------+
|            Darwin           |          112           |            120900            |               1714.7              |
|            Sydney           |          2058          |           4336374            |               1214.8              |
|           Brisbane          |          5905          |           1857594            |               1146.4              |
|            Perth            |          5386          |           1554769            |               869.4               |
|          Melbourne          |          1566          |           3806092            |               646.9               |
|            Hobart           |          1357          |            205556            |               619.5               |
|           Adelaide          |          1295          |           1158259            |               600.5               |
+-----------------------------+------------------------+------------------------------+-----------------------------------+

>>> from prettytable import MSWORD_FRIENDLY
>>> x.set_style(MSWORD_FRIENDLY)
>>> 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     |
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |

========================================================
Manually changing table style

PrettyTable has a number of style options which control various aspects of how tables are displayed. You have the freedom to set each of these options individually to whatever you prefer. The set_style method just does this automatically for you.

The options are these:

border - A boolean option (must be True or False). Controls whether or not a border is drawn around the table.
header - A boolean option (must be True or False). Controls whether or not the first row of the table is a header showing the names of all the fields.
header_style - Controls capitalisation of field names in the header. Allowed values: "cap" (capitalise first letter of each word), "title" (title case), "upper" (all upper-case), "lower" (all lower-case) or None (don't change from original field name setting). Default is None.
hrules - Controls printing of horizontal rules after rows. Allowed values: FRAME, ALL, NONE - note that these are variables defined inside the prettytable module so make sure you import them or use prettytable.FRAME etc.
vrules - Controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE
align - Horizontal alignment (None, "l" (left), "c" (centre), "r" (right))
valign - Vertical alignment (None, "t" (top), "m" (middle) or "b" (bottom))
int_format - Controls formatting of integer data. This should be a string which can be placed between "%" and "d" in something like print "%d" % 42.
float_format - Controls formatting of floating point data. This should be a string which can be placed between "%" and "f" in something like print "%f" % 4.2.
padding_width - Number of spaces on either side of column data (only used if left and right paddings are None).
left_padding_width - Number of spaces on left hand side of column data.
right_padding_width - Number of spaces on right hand side of column data.
vertical_char - Single character string used to draw vertical lines. Default is |.
horizontal_char - Single character string used to draw horizontal lines. Default is -.
junction_char - Single character string used to draw line junctions. Default is +.
==========================================================

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • JetBrains PhpStorm 8註冊碼一枚 進入:C:\Users\Bruce\.WebIde80\config\colors,把附件放入該文件夾內(點擊下載) 註:如果是mac系統,則放入:/Users/bruce/Library/Preferences/WebIde80/colors目錄 ...
  • 首先,先看一下java中對ArrayList的定義代碼: 這段代碼能給我們提供的信息: 第一:ArrayList實現了RandomAccess介面,因此查詢會很快 第二:ArrayList實現了序列化和Cloneable介面,講道理應該可以進行克隆操作,但是這裡有爭議說不是,因為這樣操作是同一個對象 ...
  • 博主昨天優化了介面框架想著再添加些功能 想到對介面的性能壓力測試 在工作過程中之前都是使用的工具 如:loadrunner、jmeter 想著這次準備用python實現對介面的性能壓力測試 首先要實現這個功能就要運用到python的threading模塊 下麵是自己學習摸索出來的代碼: 這個是10個 ...
  • (String)、Object.toString()正常情況下跟String.valueOf()沒有區別。 但當Object是null的時候。toString會拋出異常、valueOf返回"null",而(String)返回null。 ...
  • Problem A: STL——靈活的線性表 Problem A: STL——靈活的線性表 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2986 Solved: 1092[Submit][Status][Web Board] Description 數 ...
  • PriorityQueue是隊列的一種,它叫做優先隊列,該類實現了Queue介面。 之所以叫做優先隊列,是因為PriorityQueue實現了Comparator這個比較介面,也就是PriorityQueue內部具有了排序方法,在offer(插入)或poll(彈出)元素的過程中,優先隊列中的數據會動 ...
  • Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 代碼如下: public class Solution { public int reverse(int n) { l ...
  • 一、類載入過程 執行時機:編譯程式——>執行程式(JVM啟動、程式運行),類載入發生在程式運行期間 各個階段:分為載入階段、連接階段(驗證、準備、解析)、初始化、使用、卸載 執行順序:大體是按以上階段依次執行,但相互間有交叉 載入——>驗證(文件格式)——>繼續載入——>解析——>驗證(元數據、位元組 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...