myfile = open('Results.txt')
title = '{0:20} {1:20} {2:20} {3:20} {4:20}'.format('Player Nickname','Matches Played','Matches Won','Matches Lost','Points')
print(title)
for line in myfile:
item = line.split(',')
points = int(item[2]) * 3
if points != 0:
result = '{0:20} {1:20} {2:20} {3:20} {4:20}'.format(item[0], item[1], item[2], item[3],points)
print(result)
嗨,那裏只需要一些幫助,知道如何正確使用.format,出於某種原因,當打印答案時。我會期待這一點。代碼未打印到.format編碼
Player Nickname Matches Played Matches Won Matches Lost Points
Leeroy 19 7 12 21
但顯示的輸出我得到的是這種
Player Nickname Matches Played Matches Won Matches Lost Points
Leeroy 19 7 12
21
21被顯示在錯誤的地方。我做錯了什麼?
看起來像'item [3]'包含換行符。你能檢查文件中的數據嗎? –