从python控制台使用grep [英] Using grep from python console

查看:93
本文介绍了从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]

这篇关于从python控制台使用grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆