Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write list of strings to file, adding newlines?

def generator():
    nums = ['09', '98', '87', '76', '65', '54', '43']
    s_chars = ['*', '&', '^', '%', '$', '#', '@',]

    data = open("list.txt", "w")
    for c in s_chars:
        for n in nums:
            data.write(c + n)
    data.close()

I would like to add a newline after every "c + n".

like image 575
b1ackzer0 Avatar asked Mar 25 '11 05:03

b1ackzer0


People also ask

How do you write a string to a file on a new line every time in Python?

Use the newline character "\n" to write a string to a file on a new line every time.

How do you write multiple lines in a text file in Python?

Use writelines() to write multiple lines to a file Add the newline character "\n" to the end of each string to write them on separate lines, otherwise the result will be in a single line.

How do you write new line data in Python?

In Python, the new line character “\n” is used to create a new line.


1 Answers

Change

data.write(c + n)

to

data.write("%s%s\n" % (c, n))
like image 104
dietbuddha Avatar answered Oct 05 '22 20:10

dietbuddha