Created
June 21, 2016 15:14
-
-
Save tuan3w/af2305dc02e4f2ee6af213c61289abd5 to your computer and use it in GitHub Desktop.
fast way to read big file line by line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import partial | |
import codecs | |
def fast_read(name, bytes): | |
with codecs.open(name, 'r', 'utf-8') as f: | |
prev = '' | |
f_read = partial(f.read, bytes) | |
for text in iter(f_read, ''): | |
if text == '': | |
return | |
if not text.endswith('\n'): | |
text, rest = text.rsplit('\n', 1) | |
text = prev + text | |
prev = rest | |
else: | |
text = prev + text[:-1] | |
prev = '' | |
for l in text.split('\n'): | |
yield l | |
def normal_read(name): | |
with codecs.open(name, 'r', 'utf-8') as f: | |
for l in f: | |
yield l | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment