natalien
natalien

Reputation: 81

Writing to a File, Adding an Additional Space

I am creating a Markov text generator that generates haikus. The function to generate the haiku itself will generate 100 haikus using a for loop. They may look something like:

line1 line2 line3 line1 line2 line3 line1 line2 line3

When I try writing these lines to the file, I want to include a space between each haiku, so it looks like:

line1
line2
line3 

line1
line2
line3

line1
line2
line3

How would I make this happen while writing to the file?

Also, sometimes it would not preserve the format... sometimes, it's written as line1line2line3

How would I structure my loop?

I've tried:

def writeToFile():
    with open("results.txt", "w") as fp:
        count = 0
        for haiku in haikuList:
            for line in haiku:
                for item in line:
                    fp.write(str(item))
                    count += 1
        print "There are", count, "lines in your file."

haikuList looks like:

[[line1,
  line2,
  line3],
 [line1,
  line2,
  line3],
 [line1,
  line2,
  line3],
 [line1,
  line2,
  line3]]

Upvotes: 0

Views: 717

Answers (3)

mhawke
mhawke

Reputation: 87134

Use str.join() like this:

def writeToFile():
    with open("results.txt", "w") as fp:
        fp.write('{}\n'.format('\n\n'.join(['\n'.join(haiku) for haiku in haikuList])))
        print "There are {} lines in your file.".format(len(haikuList)*3 + len(haikuList)-1)

This will print each line from each haiku separated by a single new line character. str.join() is also used to add new lines between each haiku. With file.write() you need to add in the new line if you want it, so I have used str.format() to do that.

Finally, the number of lines written to the file will be the same as the length of the haikuList multiplied by 3 plus len(haikuList) - 1 for the new lines between each haiku, so you don't need a counter for that.

One other thing, rather than accessing a variable external to the function, you should pass the haiku list into the writeToFile() function:

def writeToFile(haikuList):
    with open("results.txt", "w") as fp:
        fp.write('{}\n'.format('\n\n'.join(['\n'.join(haiku) for haiku in haikuList])))
        print "There are {} lines in your file.".format(len(haikuList)*3 + len(haikuList)-1)

Then call it like this:

writeToFile(haikuList)

Upvotes: 1

Ownaginatious
Ownaginatious

Reputation: 797

Assuming each haiku in your haiku list is a list of str or unicode, you could do something like this a bit more concisely.

def writeToFile():
    with open("results.txt", "w") as fp:
        count = 0
        for haiku in haikuList:
            fp.write(" ".join(haiku) + "\n"):
            count += len(haiku)
        print "There are", count, "lines in your file."

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49921

Put an fp.write("\n") after the for line loop; this will add a blank line at the end of each haiku.

If you need to add a space after each item, you could add fp.write(" ") after fp.write(str(item)).

Upvotes: 1

Related Questions