我怎样才能对齐这个文本文件?

How can I align this text file?

我将值附加到一个文本文件,我想将它与顶部的类别对齐,我有大约 500 个名称和 10 个类别,所以我无法逐行执行。我在底部发布了一个小例子。我还希望第一类最多只有 23 个字符。这是我到目前为止所做的:

   fp = open(filename, "w")
   fp.write("Name                 Team Pos Games G A Pts PIM SOG Hits BS \n===========================================================\n")
   for items in stats:
          fp.write(str(items[0][:20])+ "  " + str(items[1]))
          fp.write('\n')
   fp.close()

这是我猜我需要修复的部分:

   fp.write(str(items[0][:23])+ "  " + str(items[1]))

这是我得到的输出片段:

Name                 Team
=========================
A.J. Greer  COL
Aaron Ekblad  FLA
Adam Clendening  CLS
Adam Cracknell  FA
Adam Erne  DET
Adam Gaudette  VAN
Adam Henrique  ANH
Adam Johnson  PIT
Adam Larsson  EDM
Adam Lowry  WPG
Adam McQuaid  FA
Adam Pelech  NYI
Adrian Kempe  LA
Alan Quine  CGY
Alec Martinez  LA
Aleksander Barkov  FLA
fp.write("%-21s%-4s" % (items[0],items[1]))

经过解析和重印测试。

import re

TESTLINES = """A.J. Greer  COL
Aaron Ekblad  FLA
Adam Clendening  CLS
Adam Cracknell  FA
Adam Erne  DET
Adam Gaudette  VAN
Adam Henrique  ANH
Adam Johnson  PIT
Adam Larsson  EDM
Adam Lowry  WPG
Adam McQuaid  FA
Adam Pelech  NYI
Adrian Kempe  LA
Alan Quine  CGY
Alec Martinez  LA
Aleksander Barkov  FLA"""

print("Name                 Team")
print("=========================")
for line in TESTLINES.split("\n"):
   (name, team) = re.findall(r"^(.*?)\s+(\S+)$", line)[0]
   print("%-21s%-4s" % (name,team))