循环当前行时如何处理下一行?
How do I process the next line when looping through the current lines?
尝试将 vba 宏转换为 python。 python 中的第一步,所以不要扔石头,如果很明显。
Reading a text file
processing each line
if the line contains a keyword
I want to process the next 2 lines
(which have the data I need to extract)
如果当前(有效)行是 i,我如何 select 第 i+1 和 i+2 行?
谢谢
这是 python 中的一种方法:
with open ('text.txt') as text_file :
for line in text_file :
if 'keyword' in line :
first_line = text_file.readline ()
second_line = text_file.readline ()
print (first_line, second_line)
这里有一些代码可以帮助您入门:
with open('path/to/file') as f:
for line in f:
if 'keyword' in line:
line = f.readline() # line i+1
line = f.readline() # line i+2
不要忘记用实际值替换 path/to/file
和 keyword
(并保留周围的 ''
)
尝试将 vba 宏转换为 python。 python 中的第一步,所以不要扔石头,如果很明显。
Reading a text file
processing each line
if the line contains a keyword
I want to process the next 2 lines
(which have the data I need to extract)
如果当前(有效)行是 i,我如何 select 第 i+1 和 i+2 行?
谢谢
这是 python 中的一种方法:
with open ('text.txt') as text_file :
for line in text_file :
if 'keyword' in line :
first_line = text_file.readline ()
second_line = text_file.readline ()
print (first_line, second_line)
这里有一些代码可以帮助您入门:
with open('path/to/file') as f:
for line in f:
if 'keyword' in line:
line = f.readline() # line i+1
line = f.readline() # line i+2
不要忘记用实际值替换 path/to/file
和 keyword
(并保留周围的 ''
)