如何将一个python脚本的输出作为输入传递到另一python脚本? [英] How do I pipe output from one python script as input to another python script?

查看:939
本文介绍了如何将一个python脚本的输出作为输入传递到另一python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

script1.py从用户那里获取一个中缀表达式,并将其转换为后缀表达式,然后将其返回或将其打印到stdout

A script1.py gets an infix expression from the user and converts it to a postfix expression and returns it or prints it to stdout

script2.py从stdin获取一个后缀表达式,然后对其求值并输出值

script2.py gets a postfix expression from stdin and evaluates it and outputs the value

我想做这样的事情:

python3 script1.py | python3 script2.py

这是行不通的,您能为我指出正确的方向吗?

This doesn't work though, could you point me in the right direction as to how I could do this?

编辑-

以下是有关无效"的更多详细信息.

here are some more details as to what "doesn't work".

当我执行 python3 script1.py | python3 script2.py 终端应在要求输入script1.py程序并将其重定向为script2.py的输入时要求我输入script2.py程序.

When I execute python3 script1.py | python3 script2.py the terminal asks me for input for the script2.py program, when it should be asking for input for the script1.py program and redirecting that as script2.py's input.

因此,当要求输入输入后缀表达式:"并将其重定向到后缀脚本时,它要求我输入后缀表达式:".

So it asks me to "Enter a postfix expression: ", when it should be asking "Enter an infix expression: " and redirect that to the postfix script.

推荐答案

如果我正确地理解了您的问题,则您的两个脚本各自会提示输入.例如,它们都可能是这样的:

If I undestand your issue correctly, your two scripts each write out a prompt for input. For instance, they could both be something like this:

in_string = input("Enter something")
print(some_function(in_string))

some_function是根据输入字符串(在每个脚本中可能有所不同)而具有不同输出的函数.

Where some_function is a function that has different output depending on the input string (which may be different in each script).

问题在于,当将一个脚本的输出通过管道传递到另一个脚本时,"Enter something"提示无法正确显示给用户.那是因为提示被写入标准输出,所以第一个脚本的提示通过管道传输到第二个脚本,而第二个脚本的提示则显示出来.这是一个误导,因为它是第一个直接(直接)接收用户输入的脚本.提示文本也可能使两个脚本之间传递的数据混乱.

The issue is that the "Enter something" prompt doesn't get displayed to the user correctly when the output of one script is being piped to another script. That's because the prompt is written to standard output, so the first script's prompt is piped to the second script, while the second script's prompt is displayed. That's misleading, since it's the first script that will (directly) receive input from the user. The prompt text may also mess up the data being passed between the two scripts.

没有完美的解决方案来解决此问题.一种部分解决方案是将提示写入标准错误,而不是标准输出.这将使您同时看到两个提示(尽管您实际上只能响应其中一个提示).我认为您不能直接使用input进行此操作,但是print可以根据需要写入其他文件流:print("prompt", file=sys.stderr)

There's no perfect solution to this issue. One partial solution is to write the prompt to standard error, rather than standard output. This would let you see both prompts (though you'd only actually be able to respond to one of them). I don't think you can directly do that with input, but print can write to other file streams if you want: print("prompt", file=sys.stderr)

另一种部分解决方案是检查您的输入和输出流,如果其中一个不是"tty"(终端),则跳过打印提示.在Python中,您可以执行sys.stdin.isatty().如果许多命令行程序直接连接到用户而不是管道或文件,则它们具有不同的交互模式".

Another partial solution is to check if your input and output streams and skip printing the prompts if either one is not a "tty" (terminal). In Python, you can do sys.stdin.isatty(). Many command line programs have a different "interactive mode" if they're connected directly to the user, rather than to a pipe or a file.

如果管道输出是您程序的主要功能,则您可能永远不想使用提示!许多标准的Unix命令行程序(例如catgrep)根本没有任何交互行为.它们要求用户传递命令行参数或设置环境变量来控制它们的运行方式.这样一来,即使他们无法访问标准输入和标准输出,也可以按预期工作.

If piping the output around is a main feature of your program, you may not want to use prompts ever! Many standard Unix command-line programs (like cat and grep) don't have any interactive behavior at all. They require the user to pass command line arguments or set environment variables to control how they run. That lets them work as expected even when they don't have access to standard input and standard output.

这篇关于如何将一个python脚本的输出作为输入传递到另一python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆