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 "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)
无论如何,这里还有另一个问题。NaN被视为True在 Python 代码中转换为布尔值时。因此,如果它是 NaN,to_address赋值将不会回退到表达式。or因此,您应该combine_first在有意义的情况下选择相关列 ( final_email['fba_to__notifications'].combine_first(final_email['lsp_escalation_back_up'].fillna('no_address@rs-components.com')),或者明确测试 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"
添加回答
举报