繁体   English   中英

相当于linux命令grep -A的python代码是什么?

[英]What is the python code equivalent to linux command grep -A?

如何使用python从文件中的匹配字符串后打印n行?

Linux命令grep

 abc@xyz:~/Desktop$ grep -A 10 'foo' bar.txt
      foo
      <shippingcost>
        <amount>3.19</amount>
        <currency>EUR</currency>
      </shippingcost>
      <shippingtype>Normal</shippingtype>

      <quality>GOOD</quality> 
      <unlimitedquantity>false</unlimitedquantity>
      <isrsl>N</isrsl> 
      <stock>1</stock>

该命令将在文件bar.txt中的匹配字符串'foo'之后打印10行

使用Python如何做同样的事情?

我试过的

import re
with open("bar.txt") as origin_file:
for line in origin_file:
    line= re.findall(r'foo', line)
    if line:
        print line

上面的Python代码给出以下输出:

abc@xyz:~/Desktop$ python grep.py
['foo']

诸如origin_file类的file对象是迭代器。 您不仅可以使用

for line in origin_file:

但您也可以使用next(origin_file)从迭代器中获取下一项。 实际上,您可以在for-loop在迭代器上调用next

import re

# Python 2
with open("bar.txt") as origin_file:
    for line in origin_file:
        if re.search(r'foo', line):
            print line,
            for i in range(10):
                print next(origin_file),

# in Python 3, `print` is a function not a statement
# so the code would have to be change to something like
# with open("bar.txt") as origin_file:
#     for line in origin_file:
#         if re.search(r'foo', line):
#             print(line, end='')
#             for i in range(10):
#                 print(next(origin_file), end='')

如果找到最后一个foo之后没有多余的10行,则上面的代码将引发StopIteration错误。 要处理这种可能性,可以使用itertools.islice从迭代器中分割最多 10个项目:

import re
import itertools as IT

with open("bar.txt") as origin_file:
    for line in origin_file:
        if re.search(r'foo', line):
            print line, 
            for line in IT.islice(origin_file, 10):
                print line,

现在,即使foo之后没有10行,代码也会正常结束(不引发StopIteration异常)。

那是因为您分配给line,并且您没有从文件对象中读取这些行,请将其更改为:

import re
with open("bar.txt") as origin_file:
for line in origin_file.readlines():
    found = re.findall(r'foo', line)
    if found:
        print line

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM