I have a txt file,
aaaa
#
bbbb
cccc
#
dddd
I only want to read the content(bbbb,cccc) line by line in the middle of '#',my code need two loops, first find the begin and end, then add for-loop in finding content, is there better way can only use one loop? because my content in txt is very long, so two-loops is not efficient...
with open("test.txt", "r") as f:
lines = f.read().splitlines()
begin = 0
for i in range (0, len(lines)):
if '#' in lines[i]:
if i > begin:
begin = i
if begin != 0:
end = i
output = []
for i in range(begin + 1, end):
output.append(lines[i])