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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...