1 python終端打印表格
使用PrettyTable庫可以較爲簡單的完成一個終端表格的打印,在引用PrettyTable庫後具體使用的關鍵代碼,可以總結爲如下五行:
x = PrettyTable(["行號", "序號", "描述", "關鍵字", "鍵值"])
x.align["行號"] = "|" # Left align city names
x.padding_width = 0 # One space between column edges and contents (default)
x.add_row([f.lineno(), index, "未知選項", key, value])
print(x)
如下圖所示是利用PrettyTable封裝的一個靜態方法:
from prettytable import PrettyTable
@staticmethod
def listConfigs(detailflag=False, settingPath='../conf/setting.properties'):
with fileinput.input(settingPath) as f:
x = PrettyTable(["行號", "序號", "描述", "關鍵字", "鍵值"])
x.align["行號"] = "|" # Left align city names
x.padding_width = 0 # One space between column edges and contents (default)
index = 0
for line in f:
if ('#' in line or line in ['\n','\r\n'] or '=' not in line):
continue
key=line.split('=')[0]
value = line.split('=')[1]
unknownList=[[]]
if (SettingPropertiesModifier.configDir.get(key) is not None):
####print(f.filename(), f.lineno(), line, end='')
SettingPropertiesModifier.configDataDic[key] = value
x.add_row([f.lineno(), index, SettingPropertiesModifier.configDir.get(key),key, value])
index+=1
elif detailflag is True:
x.add_row([f.lineno(), index, "未知選項", key, value])
index += 1
print(x)