python代码相当于linux命令grep-A是什么?
如何使用python打印文件中匹配字符串后的n行 Linux命令greppython代码相当于linux命令grep-A是什么?,python,Python,如何使用python打印文件中匹配字符串后的n行 Linux命令grep abc@xyz:~/Desktop$ grep -A 10 'foo' bar.txt foo <shippingcost> <amount>3.19</amount> <currency>EUR</currency> </shippingcost> <shippi
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>
上面的Python代码提供了以下输出:
abc@xyz:~/Desktop$ python grep.py
['foo']
这是因为您指定给行,并且您没有从文件对象读取行,请将其更改为:
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
file
对象,例如origin\u file
是迭代器。您不仅可以使用
for line in origin_file:
但您也可以使用next(origin\u file)
从迭代器获取下一项。
事实上,您可以从for循环中调用迭代器上的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='')
如果没有额外的10行,上述代码将引发StopIteration
错误
在找到最后一个foo
之后。要处理这种可能性,您可以使用
要从迭代器中切掉最多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行,结果是打印了列表。此外,我没有看到任何行数,因此您的代码中没有任何东西可以解决问题。请注意,有更好的方法可以在Python中解析XML文件。这没有在匹配后打印给定时间段内的所有行的-A
效果,这些附加行是否与目标匹配。
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,