-2

I need to be able to make python create a new text document that shows the list in a certain way and I am unsure of how to use formatting within the text creation area

def write_to_file(filename, character_list):
### Using a while loop to iterate over the list of lists (characters).
index = 0
while index < len(character_list):
    with open("new_characters.txt", "w") as output:
        output.write(str(character_list))
    index = index + 1

The code above is what I have made to get the full list to show in the text document but it just puts it all in one line.

I am required to have it set up like this:

Wonder Woman

Diana Prince

h 5 5 0 0 90

Batman

Bruce Wayne

h 6 2 0 4 80

instead of:

[['Wonder Woman', 'Diana Prince', 'h', 5, 5, 0, 0, 90], ['Batman', 'Bruce Wayne', 'h', 6, 2, 0, 4, 80],

This is the output from the code posted above.

And the code must be in a loop!

Jayden
  • 11
  • 7
  • 1
    You're opening the file each time and rewriting it from scratch with the entire list as many times as there are elements – Mad Physicist Nov 04 '21 at 04:54
  • SO is not a free tutorial site. You posted code, but offer no indication of which part is problematic or confusing. – Mad Physicist Nov 04 '21 at 04:56
  • Welcome to Stack Overflow. Did you attempt to diagnose the problem? For example, what exactly do you expect `str(character_list)` to do? In your own words, what do you think are the logical steps to solving the problem? When you are given the list, what is the first thing you should write to the file? The second? etc.? Does the list have some particular interesting internal structure? – Karl Knechtel Nov 04 '21 at 04:57
  • Ok so I forgot to mention. This list is within a list. So for example character_list[0][0] would be Wonder Woman. I'm very new to python and am not really sure how I can combine functions such as grabing the name then creating a new line. – Jayden Nov 04 '21 at 05:02
  • If you do `for character in character_list:`, then `character[0]` is the first element, `character[1]` is the second element, and so on. Three print statements and you're done. – Tim Roberts Nov 04 '21 at 05:02

3 Answers3

0

Try this.

def write_to_file(filename, character_list):
    ### Using a while loop to iterate over the list of lists (characters).
    index = 0
    while index < len(character_list):
        with open("new_characters.txt", "w") as output:
            for item in character_list:
                for character in item:
                    output.write(str(character) + '\n')
        index = index + 1
bituniverse
  • 98
  • 11
0

This works for the subset you have shown in the format you have asked for.

def write_to_file(filename, character_list):

    # open file with given filename, in 'write' mode
    with open(filename, 'w') as f:

        # iterate over characters in for loop
        # using tuple unpacking
        for (hero_name, char_name, *data) in character_list:

            # write hero and character names on a line each
            f.write(hero_name + '\n') # e.g. 'Wonder Woman'
            f.write(char_name + '\n') # e.g. 'Diana Prince'

            # convert all remaining elements to a string
            # using list comprehension
            data = [str(i) for i in data]

            # create a single string from a list of values separated by a space
            # using string join method on the list
            data = ' '.join(data)

            # write to file with newline
            f.write(data + '\n') # e.g. 'h 5 5 0 0 90'

The key components of this are tuple unpacking, list comprehensions, and the string join method. I also included the use of the filename argument to actually be used when opening a file. This means you have to pass a filename with an extension to the function call if you weren't already.

fam-woodpecker
  • 500
  • 2
  • 10
-2

Try this way:-

  • For loop would be a better option
  • Use\n for new line
def write_to_file(filename, character_list):
    with open(f"{filename}.txt", "w") as output:
        for characters in character_list:
            for character in characters:
                character =str(character)
                output.write(character+("\n" if len(character)>1 else "" ))
                #output.write(character+("\n" if len(character)>1 else " " )) for  --> h 5 5 0 0 9 0

write_to_file('Any',[['Wonder Woman', 'Diana Prince', 'h', 5, 5, 0, 0, 90], ['Batman', 'Bruce Wayne', 'h', 6, 2, 0, 4, 80]])

Output:

Wonder Woman
Diana Prince
h550090
Batman
Bruce Wayne
h620480
Bibhav
  • 1,579
  • 1
  • 5
  • 18