Re: generator to write N lines to file

2019-06-24 Thread Sayth Renshaw
> To get the individual lines you have to yield them > > for line in lines_gen: > yield line > > This can be rewritten with some syntactic sugar as > > yield from lines_gen > > > for line in getWord(fileName, 5): > > with open(dumpName, 'a') as f: > >

Re: generator to write N lines to file

2019-06-23 Thread Peter Otten
Sayth Renshaw wrote: > Afternoon > > Trying to create a generator to write the first N lines of text to a file. > However, I keep receiving the islice object not the text, why? > > from itertools import islice > > fileName = dir / "7oldsamr.txt" > dumpName = dir / "dump.txt" > > def getWord(in

generator to write N lines to file

2019-06-23 Thread Sayth Renshaw
Afternoon Trying to create a generator to write the first N lines of text to a file. However, I keep receiving the islice object not the text, why? from itertools import islice fileName = dir / "7oldsamr.txt" dumpName = dir / "dump.txt" def getWord(infile, lineNum): with open(infile, 'r+')