如何使用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>
此命令将在匹配的字符串&#39; foo&#39;之后打印10行。来自文件bar.txt
使用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']
答案 0 :(得分:2)
file
对象(例如origin_file
)是迭代器。您不仅可以使用循环浏览其内容
for line in origin_file:
但您也可以使用next(origin_file)
从迭代器中获取下一个项目。
实际上,您可以在next
:
for-loop
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='')
如果没有10行,上面的代码会引发StopIteration
错误
在找到最后一个foo
之后。要处理这种可能性,您可以使用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,
现在代码将优雅地结束(不会引发StopIteration
异常)即使存在
在foo
之后不是10行。
答案 1 :(得分:0)
这是因为您指定了行,而您没有从文件对象中读取行,请将其更改为:
Content-Type