标准的Python字符串格式可能就足够了。
# assume that your data rows are tuplestemplate = "{0:8}|{1:10}|{2:15}|{3:7}|{4:10}" # column widths: 8, 10, 15, 7, 10print template.format("CLASSID", "DEPT", "COURSE NUMBER", "AREA", "TITLE") # headerfor rec in your_data_source: print template.format(*rec)要么
# assume that your data rows are dictstemplate = "{CLASSID:8}|{DEPT:10}|{C_NUM:15}|{AREA:7}|{TITLE:10}" # same, but namedprint 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)使用对齐,填充和精确格式说明符来获得最佳结果。



