Python庫: PrettyTable 模組
一,PrettyTable簡介
PrettyTable是python中的一個第三方庫,可用來生成美觀的ASCII格式的表格:
二,PrettyTable安裝
使用PIP即可十分方便的安裝PrettyTable,如下:
pip install prettytable
三,PrettyTable匯入
from prettytable import PrettyTable
四,PrettyTable使用:
建立表:
tb = pt.PrettyTable()
按行/列新增資料:
tb.add_row(
tb.add_column(
GitHub上有詳細示例說明,按行/列新增資料:
按行新增資料
... 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)
+-----------+------+------------+-----------------+
| 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 |
+-----------+------+------------+-----------------+
按列新增資料
... tb.add_column('index',[1,2,3,4])
print(tb)
+-----------+------+------------+-----------------+-------+
| City name | Area | Population | Annual Rainfall | index |
+-----------+------+------------+-----------------+-------+
| Adelaide | 1295 | 1158259 | 600.5 | 1 |
| Brisbane | 5905 | 1857594 | 1146.4 | 2 |
| Darwin | 112 | 120900 | 1714.7 | 3 |
| Hobart | 1357 | 205556 | 619.5 | 4 |
+-----------+------+------------+-----------------+-------+
五,例子
顯示12306餘票資訊:
! /usr/bin/env python
-- coding: utf-8 --
"""
@version: conda4.5.10-py36_0
@author: geoffrey
@file: test.py
@time: 18-9-19 上午12:34
"""
from prettytable import PrettyTable
from common.trainstation import train_stations # 靜態車站資訊編碼表
class Test:
# 爬取的部分測試資料
ticket_info = [
"U%2B082uSGA2pSWBSA3FVsSCR3UBUGmHnyl%2FtN57U0VI1x%2FSnrIkzeGCeeanVDinTaoyUMPLgWVJId%0AfQPCh9%2BhBwTLxOerOSLYE7d6JdGDl%2Bwdg05n7KB4fqk%2FO1dZYn924zgcW0vhWWM6ZYlM%2F5%2FDOe3z%0AnbLpqTTq50KOIV23cipZlb%2FaMTEeLyjs5dOW%2BUGUacyxUQK7x727Up9QK9V%2BHvOU8pdBDSmbr4ib%0AL3r3xe54qZ%2FjoeAgSVaOXQW1wV1s|預訂|26000K772633|K7727|HDP|QTP|BXP|TJP|00:42|02:33|01:51|Y|a4UStvZfh7eHzFDH9BgB9u6zuFwqnYoU%2FewbjLRpqhBxlP8u|20180925|3|P2|10|11|0|0|||||||無||4|有|||||101030|113|0",
"X71DbdFMg2511fazGedRQ0S1wlDmuQXD97f8esrHOYg6ynd2B0A74q3Nn6uwjefc%2Bi%2FEKJVsqwrg%0A83scbpsvksOtyxLPpPanoeuW3H7bZzESv5qbFLLzJjCUAaxAKrANq4TruaUzDc%2FGc%2BScxCd82%2FQ3%0AROZbTW%2F6TDy%2FynpiXCyp9U0dObwHsTPbaVwnThnqM%2F5%2BI6NEFMm3WlF6BhhOBBpu75XMUpq0WEhc%0AfQn4KfBkKMcchaZlMxtgXekxcBAuekI%2FcQ%3D%3D|預訂|280000260415|2601|SUV|QTP|BJP|TJP|04:41|06:52|02:11|Y|qCwhgaXY9quSI6QM4jDQ9%2BFobKdYQ70IHf9WcIlSU1H4vLh1L6LE87ASd%2Bg%3D|20180925|3|V2|09|11|0|0||||無|||無||有|有|||||10403010|1431|0",
"TleXz4Xce%2F9LGP51j3AYp1E2XYdTQ%2B9EW2IHnKDC3UlLRNsZT13YccPSzAFpm1Tsqe6J9Og3UlHQ%0AVysv7bwWFg5RMXinS8RKv5pj94CEv3LTP7T6ZFldHhzLqyu05O8jAyc7ZTy%2BtAO%2FP0PXJTiqAdkk%0ADbEgOWg9Y1Dh2pn4voYzl7WkfxoHl%2BeRZF5M08M%2F%2FuNl%2Bwb9CaJh7v5XHwRvMN%2FRDVPR5%2FOOAIHy%0AtL0v%2FcJ1LjKIX9BErkN%2BYxYppZUG|預訂|24000C258107|C2581|VNP|YKP|VNP|TJP|06:02|06:32|00:30|Y|IzsERTpxXKsMPssO%2Br45YLgOqJ5BYzS67U2LIBBlWyHgljn9|20180926|3|P2|01|02|1|0|||||||||||有|1|5||O0M090|OM9|0",
"m%2BE9yloclL5L1T1ChwP1FozoiCSeC81xR2ihKloIeVf9uaEnoQqmk2ZEyGlqvA%2Bn4dikXQP4QWN7%0Ab7%2FIDMsAUxiGpN1WMpuucO49C9DKtLIlU6dx8khAvPqJM%2FQcmWMYGWobLy0LuS7CVLRVAmyVbUbg%0AnHGdTHlYbtSw0VztEtgg%2Ft5rYg9Rbbrg2GyN7P4RABZZG4IZal%2BrUArnHR1JkGejhfQgzrhNwxQs%0Aq06YIH12BIHp3fi62YYxdECW7QBxKzVzS0aOK9E%3D|預訂|24000C20010P|C2001|VNP|TJP|VNP|TJP|06:11|06:41|00:30|Y|qGdlluTJ5vqWoi8RthUqj9qvbdIjUxXT4QR2VOFsXsq9wO6Q%2BBs%2FAdk6s2c%3D|20180926|3|P2|01|02|1|0|||||||無||||有|3|5||O090M0O0|O9MO|0",
"dQGPuY17X4P%2FAvN3nMWIvH1GsSpfE5KeIqMXAehABrHckno7gwJCJy8cadAY3b0kfMgjCBi5Y4he%0A4Mp5ntdC2MoVePl%2F8g6Qy9GFpLqvIiRPrx54ZFQZXFTt3MIS3RnSATgstjMCwgnfd%2F%2BlsbOss7Cm%0Ao1maZpL9i83HKTa4JMd09FipAVegBx3TkGKkJ7oq0eDT8GpXoica3OOMKZF0F%2BgrtHqc26iqBMv1%0A2Dp2CK1lxxoVPgn55PPnYPikwM%2FD|預訂|24000C20030I|C2003|VNP|TJP|VNP|TJP|06:19|06:49|00:30|Y|Hd%2BAn%2FNhZ7kRZTEue7l1xwM6ARoeUaOgXptgevt1ScOCFAaQ|20180926|3|P3|01|02|1|0|||||||||||有|4|5||O0M090|OM9|1",
"GPAmA%2BbnICii7XXZxoMvkRsbHMNjmUG7OkUolTmygGFAPRLJlK7%2BY9oLhx2W%2BrK7jC8qWKMCsjc3%0AdZ6%2FcL%2BBekXCWWXP30Skks7yQk463v8xKI941L7ApIg3d7id0uaMjmjGTALGc6t7CwNXrwz%2BK4mP%0AdIPFyqOObhQdUCL%2BF%2Bkj%2BXvOZx5xxlMnA0Q1wy8%2BOYqWHgQ449FkO6eW2eQjCSL2oXUiRXtW2CyK%0Ad6y43T7djA8aqjBs8yNxV4HFWCxA|預訂|24000C22010M|C2201|VNP|TJP|VNP|TJP|06:25|07:02|00:37|Y|A25yWfeCbwq2LernKOnPBzr5wNcMSVUb147Bna%2FOG0zqW2x2|20180926|3|P2|01|03|1|0|||||||||||有|無|5||O0M090|OM9|1",
"g6dHO9xlVYDVotKQTf3ZxehNExTBLzeVFdkx5Bv6eLRdrHs%2B4S86Bq3NUtF6VxOgEBJZ40T4ZX0%2F%0A2UMFh2AcDEZHFG%2FrywU45bx793C1xq3IPi9DxXoAEYSLQpXNWpnHuRDpAXb7t3Jjk3OeIHdlvDmx%0AcZURTZfoz1RzfT%2BJcVV5xfN7Y7q%2B9c9v33GpyVvmhbz22tq5ZouuUjVg2m8w894zLsquKNgv8s4q%0Ap28NcYfja%2BpOARzlnhnYfbjz9LSA|預訂|24000C258306|C2583|VNP|YKP|VNP|TJP|06:53|07:23|00:30|Y|OXTdU5yfHQ8T6ZOUNrXcWyHgsX4F%2FMvHWQZr%2BLYfz5tch5j7|20180926|3|P3|01|02|1|0|||||||||||有|2|5||O0M090|OM9|0",
"d9Lv8wmRYC78SmTWgWauICMSWnmDjYHHhZqvieQ8YTgGWx4bCNkjhj38x4EDnFqrjDrHt3spz%2BoJ%0AtZI9nc5GuoCmHr4z6cn%2Fpn9%2BmlYNCvRcSJy1rDzyzOIQYQdr7XsCZnaLlHv502pgt0MS7BZATKH7%0Au1YCeWXpCaN66CUVl2NQMgsw3VVMu%2FV0u9VlRMZisCAGmiyMcJogrI7q3JGgtGye2ulXwYeHRNiZ%0AxWkraQypFBUuyNG1SLdB72cezXXF|預訂|24000C200912|C2009|VNP|TJP|VNP|TJP|06:59|07:29|00:30|Y|3GUlEaeZyCLmKCOUdXIDsaq7dPtTfAeeNWj9qfrroVwihjB4|20180926|3|P4|01|02|1|0|||||||||||有|無|1||O0M090|OM9|1",
"|預訂|24000000G505|G5|VNP|SHH|VNP|TIP|07:00|07:31|00:31|N|F3Cnh84G6bsbVgHks%2B0xOjl8xeCHfM8OODQowqVn8ZpsKrW8|20180926|3|P2|01|02|1|0|||||||||||無|無|無||O0M090|OM9|0"]
def _visit_tickets_info(self, ticket_info):
'''
查詢車票資訊
:return:
'''
for index, ticket_info in enumerate(ticket_info):
data_list = ticket_info.split("|")
print(data_list)
secretStr = data_list[0] # 車次加密字串
train_no = data_list[2] # 列車編號,如2400000G710Q
station_train_code = data_list[3] # 列車車次號,如G71
from_station_telecode = data_list[6] # 出發站程式碼
to_station_telecode = data_list[7] # 到達站程式碼
start_time = data_list[8] # 出發時間
arrive_time = data_list[9] # 到達時間
range_time = data_list[10] # 歷時
gr_num = data_list[21] or "--" # 高階軟臥
rw_num = data_list[23] or "--" # 軟臥
rz_num = data_list[24] or "--" # 軟座
wz_num = data_list[26] or "--" # 無座
yw_num = data_list[28] or "--" # 硬臥
yz_num = data_list[29] or "--" # 硬座
ed_num = data_list[30] or "--" # 二等座
yd_num = data_list[31] or "--" # 一等座
sw_num = data_list[32] or "--" # 商務座
dy_num = data_list[27] or "--" # 動臥
data = {
'station_train_code': station_train_code,
'from_station_name': list(train_stations.keys())[
list(train_stations.values()).index(from_station_telecode)],
'to_station_name': list(train_stations.keys())[list(train_stations.values()).index(to_station_telecode)],
'start_time': start_time,
'arrive_time': arrive_time,
'range_time': range_time,
'yd_num': yd_num,
'ed_num': ed_num,
'sw_num': sw_num,
'yz_num': yz_num,
'wz_num': wz_num,
'yw_num': yw_num,
'rw_num': rw_num,
}
yield data.values()
def _prettyPrint(self, infos):
# 建立表格
pt = PrettyTable()
# 新增表頭
pt.field_names = ('車次 出發站 到達站 出發時間 到達時間 歷時 一等座 二等座 商務座 硬座 無座 硬臥 軟臥'.split())
# 按行新增內容
for info in infos: # info 需要是一個列表
pt.add_row(info)
# 按列新增內容
pt.add_column('序號',list(range(9)))
print(pt)
def choose_tickets(self):
'''
選擇車票
:return: 車次,座位
'''
# 顯示火車票餘票資訊
tickets_info_generate = self._visit_tickets_info(self.ticket_info)
self._prettyPrint(tickets_info_generate)
if name == 'main':
c = Test()
c.choose_tickets()
輸出結果為:
+-------+--------+--------+----------+----------+-------+--------+--------+--------+------+------+------+------+------+
| 車次 | 出發站 | 到達站 | 出發時間 | 到達時間 | 歷時 | 一等座 | 二等座 | 商務座 | 硬座 | 無座 | 硬臥 | 軟臥 | 序號 |
+-------+--------+--------+----------+----------+-------+--------+--------+--------+------+------+------+------+------+
| K7727 | 北京西 | 天津 | 00:42 | 02:33 | 01:51 | -- | -- | -- | 有 | 無 | 4 | -- | 0 |
| 2601 | 北京 | 天津 | 04:41 | 06:52 | 02:11 | -- | -- | -- | 有 | 無 | 有 | 無 | 1 |
| C2581 | 北京南 | 天津 | 06:02 | 06:32 | 00:30 | 1 | 有 | 5 | -- | -- | -- | -- | 2 |
| C2001 | 北京南 | 天津 | 06:11 | 06:41 | 00:30 | 3 | 有 | 5 | -- | 無 | -- | -- | 3 |
| C2003 | 北京南 | 天津 | 06:19 | 06:49 | 00:30 | 4 | 有 | 5 | -- | -- | -- | -- | 4 |
| C2201 | 北京南 | 天津 | 06:25 | 07:02 | 00:37 | 無 | 有 | 5 | -- | -- | -- | -- | 5 |
| C2583 | 北京南 | 天津 | 06:53 | 07:23 | 00:30 | 2 | 有 | 5 | -- | -- | -- | -- | 6 |
| C2009 | 北京南 | 天津 | 06:59 | 07:29 | 00:30 | 無 | 有 | 1 | -- | -- | -- | -- | 7 |
| G5 | 北京南 | 天津南 | 07:00 | 07:31 | 00:31 | 無 | 無 | 無 | -- | -- | -- | -- | 8 |
+-------+--------+--------+----------+----------+-------+--------+--------+--------+------+------+------+------+------+
還可以美化,,,以後再說吧,,,