【问题标题】:How to read from STDIN in python from a piped grep output [duplicate]如何从管道 grep 输出中的 python 中读取 STDIN [重复]
【发布时间】:2015-08-01 09:02:16
【问题描述】:

假设我有一个文本文件,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()

[出]:

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

如何读取输入到 python 脚本中的所有行?

【问题讨论】:

  • for line in sys.stdin:
  • 谢谢@AshwiniChaudhary

标签: python linux bash grep


【解决方案1】:

正如@AshwiniChaudhary 建议的那样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 2015-10-27
    • 2016-06-05
    • 2010-11-06
    • 2013-04-27
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多