Home > other >  Python Writing a List into a Text Document With formatting
Python Writing a List into a Text Document With formatting

Time:11-04

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!

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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
  • Related