Suman Palikhe
Suman Palikhe

Reputation: 357

File Writing in python. Unwanted EOL

I am new to python. I have just writing a file in python but i am getting a unwanted new line

f.write("The X co_ordinate is "+c[0]+"  Y  is "+c[1]+
        " Peak Value : "+str(readPeakPixel(int(c[0]),int(c[1])))+"\n\n")

c[o], c[1] are String variables and readPeakPixel returns a float number. But i am getting the "Peak Value : " in new line like this

The X co_ordinate is 461  Y  is 650
 Peak Value : 85.3557

The X co_ordinate is 574  Y  is 394
 Peak Value : 534.531

The X co_ordinate is 668  Y  is 1135
 Peak Value : 487.329

Upvotes: 0

Views: 106

Answers (2)

Martin Evans
Martin Evans

Reputation: 46759

It might be clearer to first convert the two items into integers. The readPeakPixel() function can then also use those values:

c0 = int(c[0])
c1 = int(c[1].strip())

f.write("The X co_ordinate is {}  Y is {} Peak Value : {}\n\n".format(c0, c1, readPeakPixel(c0, c1)))

For example, this should write the following to your file:

The X co_ordinate is 461  Y is 650 Peak Value : 85.3557

Upvotes: 1

scytale
scytale

Reputation: 12641

use string formatting:

"The X co_ordinate is %s  Y  is %s Peak Value : %s\n\n" % \
    (c[0], c[1], readPeakPixel(int(c[0]), int(c[1])))

Upvotes: 0

Related Questions