假设示例输入是这样的:
"1 2 3"
"hello"
"hi there"
"x y z"
"5 6 7"
这应该传递到stdin,输出应该简单地将它们打印到stdout
我在 sample.py 中有以下代码,并且我使用 Eclipse 作为我的 IDE。
import fileinput
for line in fileinput.input():
print line
如果我使用命令行,我也许可以做类似的事情
python sample.py "1 2 3 \n hello"
将输入传递到标准输入。但是我应该如何在 Eclipse 上执行此操作(当需要将多行输入到 stdin 时)?
<小时/>由于我不知道如何执行此操作,因此我只是将这些行放入 txt 文件中并从中读取。
最佳答案
在 Windows 上,
- 将 Python 目录添加到环境变量 PATH(例如 C:\Python27)
- 将所有行放入某个文本文件中(例如 input.txt 应包含“hello\n hello”)
- 运行
cmd
- 进入源代码和输入文件所在目录
- 运行
python sample.py < input.txt
然后你会在 stdout
中得到输出!
关于python - 如何将多行作为输入传递给 stdin (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19150472/