I have made a generator to read a file word by word and it works nicely.
def word_reader(file): for line in open(file): for p in line.split():
for word, i in zip(word_reader(file), xrange(n)): ...