Skip to content

Instantly share code, notes, and snippets.

@sierrezinal
Forked from mfcabrera/io.py
Created May 3, 2022 18:05
Show Gist options
  • Save sierrezinal/e871b6da74769446610b0f33d3590483 to your computer and use it in GitHub Desktop.
Save sierrezinal/e871b6da74769446610b0f33d3590483 to your computer and use it in GitHub Desktop.

Revisions

  1. Miguel Cabrera created this gist May 7, 2014.
    17 changes: 17 additions & 0 deletions io.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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]