Skip to content

Instantly share code, notes, and snippets.

@mfcabrera
Created May 7, 2014 15:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mfcabrera/14015179cdfd2dd2a2fa to your computer and use it in GitHub Desktop.
Save mfcabrera/14015179cdfd2dd2a2fa to your computer and use it in GitHub Desktop.
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