Say you are reading input from a file structured like so
P3
400 200
255
255 255 255
255 0 0
255 0 0
etc...
But you want to account for any mistakes that may come from the input file as in
P3 400
200
255
255 255
255
255 0 0
255 0
0
etc...
I want to read in the first token 'P3' then the next two '400' '200' (height/width) the '255' and from here on, I want to read every token in and account for how they should be in groups of 3. I have the correct code to read this information but I can't seem to get past the wall of figuring out how to read in information by token and not by line.
Which doesn't account for an imperfect input.
Here is one way to go about it, using
csv
module:If your file consists of groups of three values (after the first
P3
item) and you cannot rely upon the line breaks to have them grouped properly, I suggest reading the file as a single string and doing the splitting and grouping yourself. Here's a straight-forward way:Using
zip
on multiple references to the same iterator is the key trick here. If you needed to handle other group sizes with the same code, you could usezip(*[it]*grouplen)
.Note that this will discard any left-over values at the end of the file if they don't form a group of three. If you need to handle that situation differently, I suggest using
zip_longest
from theitertools
module, rather than the regularzip
function. (See thegrouper
recipe in theitertools
documentation.)