As the title says, how do I read from stdin or from a file word by word, rather than line by line? I'm dealing with very large files, not guaranteed to have any newlines, so I'd rather not load all of a file into memory. So the standard solution of:
for line in sys.stdin:
for word in line:
foo(word)
won't work, since line may be too large. Even if it's not too large, it's still inefficient since I don't need the entire line at once. I essentially just need to look at a single word at a time, and then forget it and move on to the next one, until EOF.
EDIT: The suggested "duplicate" is not really a duplicate. It mentions reading line by line and THEN splitting it into words, something I explicitly said I wanted to avoid.