[8.5] how to read large files generator

. 1  # ! / Usr / bin / the env Python 
2  # - * - Coding: UTF-. 8 - * - 
. 3  
. 4  
. 5  DEF MyReadLines (F, NEWLINE):
 . 6      buf = '' 
. 7      the while True:
 . 8          the while NEWLINE in buf:
 . 9              = POS buf.index (NEWLINE)
 10              the yield buf [: POS]
 . 11              buf = buf [POS + len (NEWLINE):]
 12 is          the chunk reached, f.read = (4096 * 10 )
 13 is          IF  Not the chunk:
 14              # Description has been read end of the file, the last data processing
15             yield buf
16             break
17         buf += chunk
18 
19 
20 with open('read_file.txt') as f:
21     for line in MyReadLines(f, '{|}'):
22         print(line)
asdjkhaskjfdhaskhd
 asdjalskhdkjashgkdyoias
 asdjaslhdiasghfou
 wiuqoxgsabjkdg
 ashdoqwhdasjdha
 as;djoqwhdohqwodhg

  Data file, and a great line

1 with open(filePath) as f:
2     for line in f:
3         print(line)
4         # Processing data

  Large data files, but multiple lines

Guess you like

Origin www.cnblogs.com/zydeboke/p/11278429.html