0

I am trying to read xlsx do documents, find specific lane and print an area, but w/o printing None-lanes. Also break the cycle when "None" is in specific column.

def some():
    for r in range(1, ws.max_row):
        for c in range(1, ws.max_column):
            db = ws.cell(row=r, column=c)
            if db.value == 'this one':
                for rows in ws.iter_rows(min_row=r + 1, min_col=c - 1,
                                         max_row=r + 30, max_col=c):
                    for cell in rows:
                        if cell.value is None:
                            if column_index_from_string(db.column) == c:
                                return
                        else:
                            print(cell.value, end=" ")
                    print()

This code returns the only 1 lane and then break the head cycle. Output: 1315 text.
Format of document: https://drive.google.com/file/d/0B_bbUxPgcVESVGstWWRxV3k4emM/view. I cant resolve this issue. Please excuse for native errors. I am new python and just looking for the answer.

Ian J-Van
  • 3
  • 3
  • can you include your excel file via google drive/ dropbox and mention the rows you want to extract? – Mohammad Yusuf Jan 19 '17 at 11:54
  • @UrielEli if that really was the question being asked here, as he put "Also break the cycle when None is in specific column." then why hasn't my answer been accepted? Currently it's been down voted to -1, when I would have thought that it answers the question I just quoted. – Cephlin Jan 19 '17 at 11:56
  • @Cephlin I don't know why you have been downvoted, but its pretty much standard here not to answer such duplicates but close them, so people will be redirected to the full conversation threads. – Uriel Jan 19 '17 at 12:02
  • @UrielEli well that's fair enough, but maybe they could have left a comment informing me of this rather than blatantly down-voting a correct answer... Seems odd to down-vote a correct answer. Thanks for letting me know. – Cephlin Jan 19 '17 at 12:03
  • test document https://drive.google.com/open?id=0B_bbUxPgcVESVGstWWRxV3k4emM – Ian J-Van Jan 19 '17 at 12:06
  • @UrielEli It is not a standard though not to answer questions that have been flagged as possible duplicates by one person. – Right leg Jan 19 '17 at 12:08
  • @Cephlin Don't take down-votes personally. SO is about the content, not the person. And be prepared to be down-voted with no explanation, because it's frequent. Besides, don't be so confident about being right, especially when the question is so misleading. – Right leg Jan 19 '17 at 12:09
  • @Rightleg I should try to take down-votes personally, you're right. But I don't think I was too confident about being right as in my answer I even questioned whether I understood the question and in my comments I have also questioned this too. But you're totally right about not taking it personally. I guess it's just when you have 62 rep, losing 2 for what appears to be no reason with no explanation got to me. I'll try harder. Thanks :) – Cephlin Jan 19 '17 at 12:13
  • FWIW at least two of these loops are unnecessary. Write simple code that works first and extend it. – Charlie Clark Jan 19 '17 at 14:43

1 Answers1

0

When you check that cycle is None, instead of returning, you can call break to exit the inner loop and then have further logic before you return. You could also skip that for loop using continue. See below for an example on breaking the inner loop.

Example; this would stop the for cell in rows: loop if cell.value is None:

for r in range(1, ws.max_row):
    for c in range(1, ws.max_column):
        db = ws.cell(row=r, column=c)
        if db.value == 'My specific Value':
            for rows in ws.iter_rows(min_row=r+1, min_col=c-1, max_row=r+30, max_col=c):
                for cell in rows:
                    if cell.value is None:
                        if column_index_from_string(db.column) == c:
                            rtn = 1
                            break
                    else:
                        print(cell.value, end=" ")
                print()

return rtn

2nd Example based on breaking out of the outer for loop (r range for loop):

def print_while_cell_is_not_none():
    for r in range(1, ws.max_row):
        for c in range(1, ws.max_column):
            db = ws.cell(row=r, column=c)
            if db.value == 'My specific Value':
                for rows in ws.iter_rows(min_row=r + 1, min_col=c - 1,
                                         max_row=r + 30, max_col=c):
                    for cell in rows:
                        if cell.value is None:
                            if column_index_from_string(db.column) == c:
                                return
                        else:
                            print(cell.value, end=" ")

This second example would break the entire method and thus break out of the for r in range for loop.

Cephlin
  • 176
  • 2
  • 12
  • Guess this doesn't answer the question then since it's been voted down. I think maybe the question requires more information for me to understand what is being asked then. As far as I understood it, it was asking how to break a loop. – Cephlin Jan 19 '17 at 11:53
  • Can you please explain on my code if it is possible? – Ian J-Van Jan 19 '17 at 12:14
  • @IanJ-Van updated my answer for you. You'll have to figure out what you want to return if you need to return anything at all. – Cephlin Jan 19 '17 at 12:19
  • @Celphlin I dont need to return anything, but break the cycle when the only None is in column C – Ian J-Van Jan 19 '17 at 12:26
  • then `break` will do this. – Cephlin Jan 19 '17 at 13:03
  • @Celphin I mean the cycle for r in range – Ian J-Van Jan 19 '17 at 13:06
  • Ah if you mean in the r range, then you would have to wrap all this in a function and make it return. I'll update my answer with a second example of how this would look like @IanJ-Van – Cephlin Jan 19 '17 at 13:37
  • @Celphin The example 2 is the code that i presented in my first post. It prints the only 1 lane. I updated my post with test file and output – Ian J-Van Jan 19 '17 at 14:30
  • Then maybe remove the outer loop outside the method then. You'll have to figure it out. – Cephlin Jan 20 '17 at 14:40
  • @Celphin already done with this. There was a mistake. I used db,column instead of cell.column. Thats why the cycle was mad. Btw thx for help. – Ian J-Van Jan 21 '17 at 19:07