I'm trying to read a big file (1.1GB) into python. There will be word 'HERE' in the file. I don't know on which line I'll find the word. I read the file into chunks. My first chunk is data upto word 'HERE'. My code is working fine till here. (that is storing the data before 'HERE' and processing it) However I'm unable to proceed with reading the data after 'HERE' because the data after 'HERE' is too large. Is there any way so that I can read the data after 'HERE' line by line? I referred to the reference: Reading a file until a specific character in python My code is:
def each_chunk(stream, separator):
buffer = ''
while True: # until EOF
chunk = stream.read() # I propose 4096 or so
if not chunk: # EOF?
yield buffer
break
buffer += chunk
while True: # until no separator is found
try:
part, buffer = buffer.split(separator, 1)
except ValueError:
break
else:
yield part
def first_chunk(chunk):
.... #my function
def chunk_after(data_line_by_line):
.... #my function
global This_1st_chunk
This_1st_chunk=True
myFile= open(r"C:\Users\Mavis\myFile.txt","r")
for chunk in each_chunk(myFile, separator='HERE'):
if This_1st_chunk:
first_chunk(chunk)
This_1st_chunk=False
elif not This_1st_chunk:
print('*******after 1st chunk*********')
#**I WANT TO READ THE DATA LINE BY LINE HERE.**
chunk_after(data_line_by_line)