Varun Murthy
Varun Murthy

Reputation: 11

Unable to Format Output Text File to Desired Form using "write" function Python

I am unable to format the output of my text file as I want it. I have fooled around with this for almost an hour, to no avail, and it's driving me mad. I want the first four floats to be on one line, and the next 10 values to be delimited by new lines.

    if not (debug_flag>0):
        text_file = open("Markov.txt", "w")
        text_file.write("%.2f,%.2f,%.2f,%.2f" % (prob_not_to_not,prob_not_to_occured, prob_occured_to_not, prob_occured_to_occured))

        for x in xrange(0,10):
             text_file.write("\n%d" % markov_sampler(final_probability))

        text_file.close()

Does anyone know what the issue is? The output I'm getting is all on 1 line.

Upvotes: 0

Views: 58

Answers (1)

Sacha
Sacha

Reputation: 245

You have to put the line feed at the end of the first line for it to work. Also your text editor may be configure to have the \r\n end of line( if you are using notepad ), in wich case you should be seeing everything in the same line.

The code with the desired output may look something like this

if not (debug_flag>0):
    text_file = open("Markov.txt", "w")
    text_file.write("%.2f,%.2f,%.2f,%.2f\n" % (prob_not_to_not,prob_not_to_occured, prob_occured_to_not, prob_occured_to_occured))

    for x in xrange(0,10):
         text_file.write("%d\n" % markov_sampler(final_probability))

    text_file.close()

Upvotes: 1

Related Questions