从python控制台使用grep

从python控制台使用grep

问题描述:

如何使用python做到这一点?

Using python how can I make this happen?

python_shell$>  print myPhone.print_call_log()  |  grep 555

我所看到的唯一接近的事情是使用"ipython控制台",将输出分配给变量,然后对该变量使用.grep()函数.这不是我真正想要的.我希望对输出中的任何内容进行管道和grepping处理(包括错误/信息).

The only thing close that I've seen is using "ipython console", assigning output to a variable, and then using a .grep() function on that variable. This is not really what I'm after. I want pipes and grepping on anything in the output (including errors/info).

Python的交互式REPL没有grep,也没有处理管道,因为它不是Unix shell.您需要使用Python对象.

Python's interactive REPL doesn't have grep, nor process pipelines, since it's not a Unix shell. You need to work with Python objects.

因此,假设myPhone.print_call_log的返回值是一个序列:

So, assuming the return value of myPhone.print_call_log is a sequence:

call_log_entries = myPhone.print_call_log()
entries_containing_555 = [
        entry for entry in call_log_entries
        if "555" in entry]