2011-07-29 52 views
1

如果有人在linux上用過pexpect你注意到pexpect在使用其readline()函數時返回窗口風格的結尾?你知道一種擺脫這種方式嗎?pexpect返回windows風格行尾

回答

1

不僅readline()返回\ r \ n,pexpect返回的所有捕獲文本都包含\ r \ n行尾。

的文件有如下解釋:

readline(self, size=-1) 
This reads and returns one entire line. A trailing newline is kept 
in the string, but may be absent when a file ends with an incomplete 
line. Note: This readline() looks for a \r\n pair even on UNIX 
because this is what the pseudo tty device returns. So contrary to what 
you may expect you will receive the newline as \r\n. An empty string 
is returned when EOF is hit immediately. Currently, the size argument is 
mostly ignored, so this behavior is not standard for a file-like 
object. If size is 0 then an empty string is returned. 

你可以做這樣的事情來轉換\ r \ n爲\ n

line = child.readline() 
line.replace('\r\n', '\n')