1 回答

TA貢獻1811條經驗 獲得超5個贊
該錯誤表明庫在需要字符串的地方收到了一個浮點數。從您的代碼中,我希望其中一個body或一個字段final_email包含一個浮點數。
由于數據框中的空值,浮點數是 NaN 我不會感到驚訝。為了確保(或使您的代碼更健壯),您可以嘗試過濾異常并顯示有問題的值:
for i, row in final_email.iterrows():
subject = row["Subject"]
to_address = row['fba_to__notifications'] or row['lsp_escalation_back_up'] or "[email protected]"
cc_list = row['cc_list']
send_from="[email protected]"
try:
message = create_message(send_from,to_address, cc_list, subject, plain_text_body=body)
except AttributeError as e:
print('Error composing email', send_from,to_address, cc_list, subject, body, '\n', e)
# raise # optionaly re-raise the exception if you want to stop processing
send_message(message)
無論如何,這里還有另一個問題。NaN被視為True在 Python 代碼中轉換為布爾值時。因此,如果它是 NaN,to_address賦值將不會回退到表達式。or因此,您應該combine_first在有意義的情況下選擇相關列 ( final_email['fba_to__notifications'].combine_first(final_email['lsp_escalation_back_up'].fillna('[email protected]')),或者明確測試 NaN 值:
to_address = row['fba_to__notifications'] if not np.isnan(row['fba_to__notifications']) \
else row['lsp_escalation_back_up'] if not isnan(row['lsp_escalation_back_up']) \
else "[email protected]"
添加回答
舉報