我的变量输出“var1”如下所示。
load: load1
a: apples
b: banana
c: oranges
d: grapes
load: load2
a: pen
b: paper
c: books
我想要的是找到第一个字符串“load:load1”并找到最后一个字符串
“d:葡萄”并打印下面的线条。
load: load1
a: apples
b: banana
c: oranges
d: grapes
为了达到这个目的,我使用的是“开始”和“结束”命令。如何使用python完成它?
答案 0 :(得分:0)
您可以执行类似的操作(假设您从文件加载):
start_line = "load: load1"
end_line = "d: grapes"
print_lines = False
with open('input.txt' , 'r') as f:
for line in f:
line = line.rstrip()
if line == start_line:
print_lines = True
if print_lines:
print line
if line == end_line:
print_lines = False
# Could add a break here if you wanted to do nothing with
# the rest of the file; e.g:
# break