简体   繁体   English

在Windows的cmd.exe Shell中,如何将Perl脚本的输出传递给Python脚本?

[英]In Windows' cmd.exe shell, how can I pipe the output of a Perl script to a Python script?

Let's say I have an example perl program, that I can run from CMD (Windows 7) using the command: 假设我有一个perl程序示例,可以使用以下命令从CMD(Windows 7)运行:

perl hello.pl

This outputs: 输出:

Hello World!

I can place this in a file using: 我可以使用以下命令将其放置在文件中:

perl hello.pl > output.txt

However, what I would like to do, is give it to another application, for this I need it to be fed to a Python application. 但是,我想做的是将它提供给另一个应用程序,为此,我需要将其提供给Python应用程序。 I give arguments to my Python application using the syntax: 我使用以下语法为我的Python应用程序提供参数:

python python.py Arg1 Arg2 Arg3

Assuming my Perl program only gives one parameter, is there a way to run a Python application once it has finished with this parameter? 假设我的Perl程序仅给出一个参数,一旦用完该参数,是否可以运行Python应用程序?

Make your python program read the output from the perl program from sys.stdin and pipe the perl program's stdout to it: 使您的python程序从sys.stdin读取perl程序的输出,并将perl程序的stdout传递给它:

perl hello.pl | python python.py Arg1 Arg2 Arg3

I tested this going from one python program to another and in the second python program just did: 我测试了从一个python程序到另一个python的运行情况,而在第二个python程序中却做了:

import sys

inp = sys.stdin.readline()
print(inp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM