文件写在python中。不需要的EOL

时间:2015-08-21 09:07:40

标签: python file

我是python的新手。我刚刚在python中编写了一个文件但是我得到了一个不需要的新行

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]是String变量,readPeakPixel返回一个浮点数。但是我在这个新行中得到了“峰值:”

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

2 个答案:

答案 0 :(得分:1)

首先将两个项目转换为整数可能更清楚。 readPeakPixel()函数也可以使用这些值:

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

例如,这应该将以下内容写入您的文件:

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

答案 1 :(得分:0)

使用字符串格式:

"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])))