Is there some way to read in information from a file by token regardless of the formatting? For example, if I am trying to generate a ppm image from an input file and instead of 2 pixels being
255 0 0
0 0 255
it appears as
255
0 0
0
0 255
I'd like to read by token in this situation instead of by line in my loop but I can't find any built in methods regarding this.
You can use
chain.from_iterable
, where the iter-able would beline.split() for line in fin
:You could always roll your own file iterator:
Then use like a normal file: