0

I should say from the outset that I have no coding experience but by reading hundreds of answers on this site I've managed to build since Friday evening an (almost) working set of code that does what I want.

However, I have now spent most of the afternoon puzzling over this and I can't find an answer either here or with a wider Google search.

I think I know what the problem is. In the code below when it finds a file to run through the code and append the notes list it also goes on to run through the else section. (Having studied the output I'm sure it would be too much of a coincidence if this wasn't what was happening). The problem is I've exhausted all the different things I can think of (or find from internet searches) and am still stuck.

notes = []
for index, row in df.iterrows():
    prefix = (row['Work Order No'])
    with os.scandir(team_folder) as files_available:
        for entry in files_available:
            if entry.name.startswith(prefix) and entry.name.endswith('.xlsx'):
                book = load_workbook(entry)
                sheet = book['Sheet1']
                sheet['A1'] = row['Forecast to Complete Gross Revenue'] if \
                    row['Forecast to Complete Gross Revenue'] > 0 else 1
                notes.append('CTC updated') if row['Forecast to Complete Gross Revenue'] > 0 else\
                     notes.append('CTC updated (no remaining revenue)')
                book.save(entry)
        else:
            notes.append('No CTC file found')

Very grateful for any assistance. This is a great site and the only reason I've got as far as I have is because of this site.

tripleee
  • 175,061
  • 34
  • 275
  • 318
OuluChris
  • 41
  • 1
  • 7
  • 2
    You probably want to indent your `else` clause – roganjosh Jul 19 '20 at 19:51
  • your else clause is attached to the for-statement, not your `if` statement. – juanpa.arrivillaga Jul 19 '20 at 19:52
  • See https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops. Due to your indentation, the `else` applies to the `for` loop and not an alternative to your `if` check. What you've written is valid, but I'd be pretty surprised if it's what you intended – roganjosh Jul 19 '20 at 19:52
  • you can remove the `else` statement, and just indent the `notes.append(...)` on same par as the if statement but at the end. – de_classified Jul 19 '20 at 19:52
  • Thanks, yeah I tried that earlier in the day but unfortunately, it makes the problem worse for reasons I don't really understand. Ultimately, I want my notes list to equal the length of the data frame I'm cross-checking it against so each Work Order Number has comments next to it. – OuluChris Jul 19 '20 at 19:57
  • Thanks for that link @roganjosh. I read that earlier in the day but didn't understand the significance of the 'break' statement. If I add break underneath the line book.save(entry) that seems to fix it! – OuluChris Jul 19 '20 at 20:10
  • Your code has a nontrivial set of undeclared external dependencies. If you want to ask about Python itself, please construct a [mre] which doesn't require external files or third-party libraries to run. See also the [help] and in particular [How to ask.](/help/how-to-ask) – tripleee Jul 20 '20 at 19:48

1 Answers1

0

Your indentation is wrong.

You are building a for/else ... thing:

wrong indentation

I guess you want the else to belong to the if, and not the for, so indent it further to align with the if:

correct indentation

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • At the end of this, the number of entries in the notes list needs to be equal to the number of entries of work order numbers which are in the data frame being iterated through. At the moment there are 116 work order numbers and where I have the else gives 121 items in the list (there are 5 test files which meet the criteria of the if statement which is why I think it's too big a coincidence for it not to be related as I described). Moving the else statement gives a list of 1,392 entries! Maybe the whole thing is just junk but I'd sure like to understand why it's junk :-) – OuluChris Jul 19 '20 at 20:07
  • I've added a break statement at the bottom of the if statement and that seems to have fixed it! – OuluChris Jul 19 '20 at 20:11
  • It seems like you are still not really sure what your code is actually doing when it runs - I'd highly recommend [using a debugger](https://code.visualstudio.com/docs/python/python-tutorial#_configure-and-run-the-debugger) to step through the code as it is executing, and looking at the contents of variables and such, to understand its behavior and why it does the things it does. – CherryDT Jul 19 '20 at 20:13
  • Thanks for the suggestion which I will do. You're right, I still don't fully understand it. This is very much a hobby project, basically seeing if it could be done, I have no intention of turning my code loose on my employer's data! – OuluChris Jul 20 '20 at 04:21