1 回答

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
該錯(cuò)誤表明庫(kù)在需要字符串的地方收到了一個(gè)浮點(diǎn)數(shù)。從您的代碼中,我希望其中一個(gè)body或一個(gè)字段final_email包含一個(gè)浮點(diǎn)數(shù)。
由于數(shù)據(jù)框中的空值,浮點(diǎn)數(shù)是 NaN 我不會(huì)感到驚訝。為了確保(或使您的代碼更健壯),您可以嘗試過(guò)濾異常并顯示有問題的值:
for i, row in final_email.iterrows():
subject = row["Subject"]
to_address = row['fba_to__notifications'] or row['lsp_escalation_back_up'] or "no_address@rs-components.com"
cc_list = row['cc_list']
send_from="ukrd@kuedid.com"
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)
無(wú)論如何,這里還有另一個(gè)問題。NaN被視為True在 Python 代碼中轉(zhuǎn)換為布爾值時(shí)。因此,如果它是 NaN,to_address賦值將不會(huì)回退到表達(dá)式。or因此,您應(yīng)該combine_first在有意義的情況下選擇相關(guān)列 ( final_email['fba_to__notifications'].combine_first(final_email['lsp_escalation_back_up'].fillna('no_address@rs-components.com')),或者明確測(cè)試 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 "no_address@rs-components.com"
添加回答
舉報(bào)