0

Hello everyone I'm in the final step of my program to send in calendar but I can't seem to get my for loop to perform the function over the list. It only reads for 1 of the 3 in list. Here is my code below.

Order List = ['0730049','4291200','1830470']

for eachId in Order_List:
**Code to create orders into excel sheet.**

df_list = [f'OrderId_{eachId}.xlsx']
print(df_list)
***everything works fine up until here ***      
for eachOrder in df_list:
  df = pd.read_excel (eachOrder,engine='openpyxl')
  New_Order_Date= df.iat[0,3]
  Order_obj_date= datetime.strptime(New_Order_Date,'%m/%d/%Y').date()
  print(Order_obj_date)

my df_list when I print shows me my list of .xlsx files created output :

'0730049.xlsx','4291200.xlsx','1830470.xlsx'

But now I need the New_Order_Date to read in each excel file properly and find the date which is at cell value (D,2). Currently, my formula for one excel file, the df. iat[0,3] works perfectly. How can I get it to perform that same search for each of the df_list?

hopefully, this makes sense? but the current code that works for 1 file when printed to give everyone an idea equals:

New_Order_Date=df.iat[0,3]
output: 07-01-2022

expected output;

0730049.xlsx= 07-01-2022
4291200.xlsx= 07-04-2022
1830470.xlsx= 07-27-2022
4
  • Please indent your code properly. And use # comments rather than ** text for your commentary about it. Commented Mar 23, 2022 at 18:51
  • Why is df_list a list if it always has just one element? Commented Mar 23, 2022 at 18:52
  • its not df_list is '0730049.xlsx','4291200.xlsx','1830470.xlsx' but I wrote it like that so I didnt manual have to add the order excels. Commented Mar 23, 2022 at 18:54
  • these are going to be changing based on when the orders are created and printed to excel. so it wont be the same everytime. Commented Mar 23, 2022 at 18:54

1 Answer 1

2

df_list isn't a list of all the filenames. You're overwriting it with just one filename each time through the loop.

Use a list comprehension to get all of them.

df_list = [f'OrderId_{eachId}.xlsx' for eachId in Order_List]

But you may not even need that variable, just do everything in the loop over Order_List

for eachId in Order_List:
    df = pd.read_excel (f'OrderId_{eachId}.xlsx', engine='openpyxl')
    New_Order_Date= df.iat[0,3]
    Order_obj_date= datetime.strptime(New_Order_Date,'%m/%d/%Y').date()
    print(Order_obj_date)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.