Created
May 7, 2014 15:50
-
-
Save mfcabrera/14015179cdfd2dd2a2fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def delimited(filename, delimiter=' ', bufsize=4096): | |
''' | |
Creates a generator of word from a file based on a delimiter (by default white space). | |
''' | |
buf = '' | |
with open(filename) as file: | |
while True: | |
newbuf = file.read(bufsize) | |
if not newbuf: | |
yield buf | |
return | |
buf += newbuf | |
words = buf.split(delimiter) | |
for word in words[:-1]: | |
yield word | |
buf = words[-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment