sxl icon indicating copy to clipboard operation
sxl copied to clipboard

Trying to only return 1 row from 'rows' returns list of list, but should probably return just a list.

Open ktr opened this issue 6 years ago • 4 comments

I think when you try to grab 1 row (e.g., ''ws.rows[1]'' to grab the 1st row of a worksheet), it should simply return that row.

ktr avatar Oct 12 '18 16:10 ktr

Well, rows is meant to be an iterator. Iterators cannot be accessed by arbitrary index, they can only be accessed one element at a time starting from the beginning.

jkyeung avatar Oct 12 '18 16:10 jkyeung

Wait a minute. Are you just leaving yourself notes for planned fixes and new features?

You should use a better example than ws.rows[1] because that would be the second row, in Python. Which I guess is kinda-sorta the first "real" row if you have a header row and are not counting it. But for the sake of illustration, you should be saying something like "I'd like ws.row[3] to just return the fourth row in the sheet".

jkyeung avatar Oct 12 '18 17:10 jkyeung

Yes, this one is tough. I also struggled with whether ws.rows[0] should return the first row or ws.rows[1] should. ws.rows[0] is definitely more pythonic, but it always seemed like more work than necessary to always remember to translate.

Right now ws.rows[0] doesn't return anything and ws.rows[1] returns the first row, but perhaps I will/should change that.

ktr avatar Oct 12 '18 20:10 ktr

Yeah, choosing indexing when you've got one foot in a 0-based environment and the other foot in a 1-based environment is always tricky. I don't mean to dictate your choice; it's your library; but I personally prefer to stick to what Python does, especially if the code looks like it is something built into Python.

For example, in this case, ws.rows[1] looks like the native Python expression which chooses the 2nd element of a sequence. If instead it were a function or method, like ws.rows(1), then there is more leeway to have the behavior be whatever you want, since anything goes in a function or method. There is no predefined Python meaning for those parentheses, other than "call some function with some argument".

But either option has benefits and drawbacks. I'll note that xlrd is 0-based and openpyxl is 1-based, so you've got precedent either way.

jkyeung avatar Oct 12 '18 21:10 jkyeung