ExcelFiles.jl icon indicating copy to clipboard operation
ExcelFiles.jl copied to clipboard

Cannot iterate over ExcelFile

Open simonbyrne opened this issue 6 years ago • 3 comments

t = load(filename, sheet)
for row in t
   @show row
end

gives

ERROR: MethodError: no method matching iterate(::ExcelFiles.ExcelFile)
Closest candidates are:
  iterate(::Core.SimpleVector) at essentials.jl:589
  iterate(::Core.SimpleVector, ::Any) at essentials.jl:589
  iterate(::ExponentialBackOff) at error.jl:171
  ...
Stacktrace:
 [1] top-level scope at ./none:0

simonbyrne avatar Jan 22 '19 21:01 simonbyrne

You need to call IteratorInterfaceExtensions.getiterator on t and then iterate over the thing that is returned by that (see here):

t = load(filename, sheet)
for row in getiterator(t)
   @show row
end

should work.

We could probably also change things here so that the thing returned by load can be iterated directly. It would change the story a bit, though, because right now load is essentially lazy, and we wanted to make the thing returned by load be iterable, it would need to have all the necessary column information in its type, and for that it would have to look at the content of the file...

davidanthoff avatar Jan 23 '19 01:01 davidanthoff

Ah, I wasn't aware of IteratorInterfaceExtensions. I just found it odd that you could call collect on it, but not iterate over it (since collect is defined as "collecting an iterator").

simonbyrne avatar Jan 23 '19 17:01 simonbyrne

Yeah, that is probably not super consistent... But I use collect so often, that I just added a method to collect. That in turn then calls getiterator and the normal collect.

davidanthoff avatar Jan 23 '19 23:01 davidanthoff