Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python 2中抑制回车?_Python_Io - Fatal编程技术网

如何在python 2中抑制回车?

如何在python 2中抑制回车?,python,io,Python,Io,当我使用它将exe的输出重定向到我自己的文件时,它总是 每行后面都有回车符,如何抑制它?下面是我如何删除回车符的: 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)],std

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


每行后面都有回车符,如何抑制它?

下面是我如何删除回车符的:

        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,

很好。

Hi,删除'\n'不需要使用
line.strip()
,因为拆分行已经完成了任务:
'one\ntwo\ntree'。拆分行()-->['one','two','three']
True。但是也要考虑,条()也去除了诸如空间的超前或后置空白字符。也就是说,您可能想要,也可能不想要,这取决于您的用例。
 p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0]
 for line in p.splitlines():
 if line.strip():
     print line
def Popenstrip(self):
  p = Popen([vmrun_cmd, list_arg], stdout=PIPE).communicate()[0]
  return (line for line in p.splitlines() if line.strip())
print line.rstrip('\r\n')