如何在python 2中抑制回车?

时间:2009-05-19 05:59:40

标签: python io

        myfile = open("wrsu"+str(i)+'_'+str(j)+'_'+str(TimesToExec)+".txt",'w')
        sys.stdout = myfile
        p1 = subprocess.Popen([pathname,"r", "s","u",str(i),str(j),str(runTime)],stdout=subprocess.PIPE)
        output = p1.communicate()[0]
        print output,

当我使用它将exe的输出重定向到我自己的文件时,它总是

每行后回车,如何压制?

3 个答案:

答案 0 :(得分:2)

以下是我删除回车的方法:

 p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0]
 for line in p.splitlines():
 if line.strip():
     print line

答案 1 :(得分:1)

def Popenstrip(self):
  p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0]
  return (line for line in p.splitlines() if line.strip())

答案 2 :(得分:0)

print line.rstrip('\r\n') 

会好起来的。