从pexpect.run()返回的输出在每行末尾都包含\ r \ n。使用print(returnVal.decode())打印到终端可以为返回的每一行正确打印一行。当我检查输出时,我看到字节字符串包含\ r \ n。当我将其记录到文件中时,我得到了两次返回到日志文件的信息。我在使用Python 3.7的Mac上。写输出时,有没有办法设置首选的新行?我正在使用pythons日志记录类,并使用info()方法编写字符串。输出看起来像这样:
total 80
-rw-r--r-- 1 xxxx admin 1048 Nov 12 00:41 Constants.py
-rw-r--r-- 1 xxxx admin 5830 Nov 12 13:33 file1.py
-rw-r--r-- 1 xxxx admin 2255 Nov 12 00:51 file2.py
看起来应该像这样:
total 80
-rw-r--r-- 1 xxxx admin 1048 Nov 12 00:41 Constants.py
-rw-r--r-- 1 xxxx admin 5830 Nov 12 13:33 file1.py
-rw-r--r-- 1 xxxx admin 2255 Nov 12 00:51 file2.py
这是我原来的Logger类的简化版本:
class Logger():
def __init__( self,path ):
msgFormat = '%(asctime)s.%(msecs)d\t%(message)s'
dateFormat = '%m/%d/%Y %H:%M:%S'
logging.basicConfig( format=msgFormat,datefmt=dateFormat,filename=path,level=logging.INFO )
def Log ( self,theStr ):
logging.info( str( theStr ))
从Pexpect返回的字符串类似于:
Line1\r\nLine2