Generator to read large files application

# 500G, special line 
def myreadlines(f, newline):
  buf = ""
  while True:
    while newline in buf:
      pos = buf.index(newline)
      yield buf[:pos]
      buf = buf[pos + len(newline):]
    chunk = f.read(4096)

    if  not chunk: #Indicates
       that the end of the file has been read 
      yield buf
       break 
    buf += chunk

with open("input.txt") as f:
    for line in myreadlines(f, "{|}"):
        print (line)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671076&siteId=291194637