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
CodePudding user response:
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)