首页 > 解决方案 > 写入文件 .txt

问题描述

这就是输出在文本文件中的样子

Ray Holt
5 5 0 0 100 15
Jessica Jones
12 0 6 6 50 6
Johnny Rose
6 2 0 4 20 10
Gina Linetti
7 4 0 3 300 15

这个数字是我必须创建的游戏的结果。

我的问题是,如何将字符串和整数结果写入文本文件?我试过这个

def write_to_file(filename, player_list):
    output = open(filename, "w")    
    for player in player_list:
        output.write(str(player))

但输出是

Ray Holt                   5  5  0  0     100      15Jessica Jones             12  0  6  6      50      6Johnny Rose                6  2  0  4      20      10Gina Linetti               7  4  0  3     300      15Khang                      0  0  0  0     100       0

他们在 1 行

请帮我!非常感谢各位

标签: python

解决方案


用这个:

def write_to_file(filename, player_list):
    output = open(filename, "w")    
    for player in player_list:
        output.write(str(player)+'\n')
output.close()

推荐阅读