I'd say that either you iterate twice or you build the two output lines first and then display them:
d = {'a': 'long val', 'b': None, 'long key': 3, 'd': 4}
headers = ""
row = ""
for k, v in d.items():
if row != "":
row += " | "
headers += " | "
max_len = max(len(str(k)), len(str(v)) if v else 1)
headers += str(k).center(max_len)
row += str(v).center(max_len) if v else " "
print(headers)
print(row)
But it looks like a lot of hassle to avoid a double iteration over a simple dict.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…