Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 打印派生进程的输出会导致输出格式错误:为什么?_Python_String_Pexpect - Fatal编程技术网

Python 打印派生进程的输出会导致输出格式错误:为什么?

Python 打印派生进程的输出会导致输出格式错误:为什么?,python,string,pexpect,Python,String,Pexpect,我正试图使用pexpect库从另一个python程序执行python程序,但我没有得到预期的行为 我希望第一个程序(prog1.py)的输出实时显示在第二个程序(prog2.py)的终端上。使用child。在之后,我得到的输出格式不正确:所有\n和\r都在输出中打印,而不是正确地用作行尾 child = pexpect.spawn('python3 /home/robb/Workspace/prog1.py') child.expect(".*do:") child.sendline(sys.

我正试图使用
pexpect
库从另一个python程序执行python程序,但我没有得到预期的行为

我希望第一个程序(
prog1.py
)的输出实时显示在第二个程序(
prog2.py
)的终端上。使用
child。在
之后,我得到的输出格式不正确:所有
\n
\r
都在输出中打印,而不是正确地用作行尾

child = pexpect.spawn('python3 /home/robb/Workspace/prog1.py')

child.expect(".*do:")
child.sendline(sys.argv[1])
print(child.after)
我在一行中获得所有输出:

b'Initializing database...\r\nDone initializing database!\r\n******************************\r\nProgram running\r\n******************************\r\n1. First Option\r\n2. Second Option\r\n\r\nPlease input number of action you want to do:'
此外,问题的答案(在本例中为
sys.argv[1]
)甚至没有出现。 如何正确显示
prog1
的输出

使用
print(child.before)
我得到了更糟糕的输出,简单地说:

b''

child.after
返回一个
bytes
类型,而不是预期的
str

将输出转换为
str

print(child.after.decode('utf8'))

child.after
返回一个
bytes
类型,而不是预期的
str

将输出转换为
str

print(child.after.decode('utf8'))

输出作为对象存储在
child.after
中。要获得ASCII输出,请对其进行相应解码:

打印(子.after.decode('ascii'))

输出作为对象存储在
child.after
中。要获得ASCII输出,请对其进行相应解码:

打印(子.after.decode('ascii'))