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)