I have a large text file which have values separated by a header starting with "#". If the condition matches the one in the header I would like to read the file until the next header "#" and SKIP rest of the file.
To test that I'm trying to read the following text file named as test234.txt:
# abcdefgh
1fnrnf
mrkfr
nfoiernfr
nerfnr
# something
njndjen kj
ejkndjke
#vcrvr
The code I wrote is:
file_t = open('test234.txt')
cond = True
while cond:
for line_ in file_t:
print(line_)
if file_t.read(1) == "#":
cond = False
file_t.close()
But, the output I'm getting is:
# abcdefgh
fnrnf
rkfr
foiernfr
erfnr
something
jndjen kj
jkndjke
vcrvr
Instead I would like the output between two headers separated by "#" which is:
1fnrnf
mrkfr
nfoiernfr
nerfnr
How can I do that? Thanks!
EDIT: Reading in file block by block using specified delimiter in python talks about reading file in groups separated by headers but I don't want to read all the headers. I only want to read the header where a given condition is met and as soon as the line reaches the next header marked by '#' it stops reading the file.