一 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 |
+-----------+------+------------+-----------------+
- 按列添加数据
```bash
>>> 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 |
+-------+--------+--------+----------+----------+-------+--------+--------+--------+------+------+------+------+------+
还可以美化,,,以后再说吧,,,