Standard Python string formatting就足够了。
# assume that your data rows are tuples
template = "{0:8}|{1:10}|{2:15}|{3:7}|{4:10}" # column widths: 8, 10, 15, 7, 10
print template.format("CLASSID", "DEPT", "COURSE NUMBER", "AREA", "TITLE") # header
for rec in your_data_source:
print template.format(*rec)
要么
# assume that your data rows are dicts
template = "{CLASSID:8}|{DEPT:10}|{C_NUM:15}|{AREA:7}|{TITLE:10}" # same, but named
print template.format( # header
CLASSID="CLASSID", DEPT="DEPT", C_NUM="COURSE NUMBER",
AREA="AREA", TITLE="TITLE"
)
for rec in your_data_source:
print template.format(**rec)
使用对齐,填充和精确格式说明符来获得最佳结果。