簡體   English   中英

用python編寫文件。 有害的停產

[英]File Writing in python. Unwanted EOL

我是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]是字符串變量,readPeakPixel返回浮點數。 但是我在這樣的新行中得到了“ Peak Value:”

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

首先將兩項轉換為整數可能會更清楚。 然后, 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

使用字符串格式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM