Write to File. Txt

Write to File. Txt

This is how the output should look like in the text file

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

The number is the result from the game that I have to create.

My question is, how can I write to the text file with both string and integer result ? I have tried this

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

but the output is

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

They are in 1 line

Please help me! Thanks a lot guys

5

1 Answer

Use this:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.