1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 def MyReadLines(f, newline): 6 buf = '' 7 while True: 8 while newline in buf: 9 pos = buf.index(newline) 10 yield buf[:pos] 11 buf = buf[pos + len(newline):] 12 chunk = f.read(4096*10) 13 if not chunk: 14 # 说明已经读到了文件结尾,处理最后一条数据 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
文件中的数据,一行且很大
1 with open(filePath) as f: 2 for line in f: 3 print(line) 4 # Processing data
文件数据大,但是多行