smac89
6/11/2016 - 6:15 AM

Efficient method to read words from a file.

Efficient method to read words from a file.

import itertools

def readwords(file_object):
    byte_stream = itertools.groupby(
    	itertools.takewhile(lambda c: bool(c),
    		itertools.imap(file_object.read,
    			itertools.repeat(1))), str.isspace)

    return ("".join(group) for pred, group in byte_stream if not pred)
        
# Example usage
import sys

if __name__ == '__main__':
    
    # read from a user file
    with open(sys.argv[1], 'r') as f:
        for w in readwords(f):
            print (w)
            
    
    # read from stdin
    for w in readwords(sys.stdin):
        print (w)