如何在Python中从管道grep输出中读取STDIN

4

假设我有一个文本文件,grep "@" file.txt 命令返回:

(1) preparing corpus @ Tue Apr 28 20:19:31 CEST 2015
(1.0) selecting factors @ Tue Apr 28 20:19:31 CEST 2015
(1.2) creating vcb file /media/2tb/ccexp/phrase-mkcls-mgiza-10clusters/work.en-ru/training/corpus/en.vcb @ Tue Apr 28 20:19:31 CEST 2015

我想使用我的Python脚本来读取输出,类似于以下方式:

grep "@" file.txt | python process.py

我尝试了这个(process.py),但它只读取了第一行:
import sys
logfile = raw_input()
print raw_input()

[out]:

(1) preparing corpus @ Tue Apr 28 20:19:31 CEST 2015

如何读取所有通过管道传递给Python脚本的行?

2
for line in sys.stdin: - Ashwini Chaudhary
感谢 @AshwiniChaudhary - alvas
1个回答

0

正如@AshwiniChaudhary所建议的:

import sys
log = "".join([i for i in sys.stdin])
print log

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接