0

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]]
natalien
  • 81
  • 3
  • 13

3 Answers3

1

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)).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

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."
Ownaginatious
  • 787
  • 4
  • 14
  • Thanks for your answer! I actually made some edits to my question. Realized that I wasn't so clear before! – natalien May 09 '16 at 01:11
1

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)
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Hi, so maybe I worded my question a little bit wrong. This works out great in terms of getting each haiku on separate lines. However, I'm looking for a blank line in between each haiku and also for each line of the haiku to be on a separate line itself. I'm editing my question to make it more clear! – natalien May 09 '16 at 01:07
  • @natalien: you could try to adapt the answer to your change of requirements. I have updated the answer which is to use `'\n'.join()` instead of `' '.join()` and to add an extra new line between haikus. – mhawke May 09 '16 at 01:17
  • yeah, I was just confused about how to organize that. thanks! – natalien May 09 '16 at 04:38