从 python 发送电子邮件时获取“浮动”对象没有属性“编码”

当我从 python 发送电子邮件时,出现错误“浮动”对象没有属性“编码”。这成功运行了 6-7 天,没有任何问题。


def create_message(send_from, send_to, cc_to, subject, plain_text_body):

    

    message = MIMEMultipart('alternative')

    message['From'] = send_from

    

    message['To'] =send_to    

    message['Cc'] = cc_to

    message['Date'] = formatdate(localtime=True)

    message['Subject'] = subject

    message.attach(MIMEText(plain_text_body, 'plain'))

    return message


def add_attachment_from_local_disk(message, path):

    with open(path, "rb") as file:

        part = MIMEApplication(file.read(),Name=basename(path))

        part['Content-Disposition'] = 'attachment; filename="%s"' % basename(path)

        message.attach(part)

        

def send_message(message):

    print(message)

    client = boto3.client("ses",region_name='eu-west-1')

    response = client.send_raw_email(RawMessage = {"Data": message.as_string()})


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"

    message = create_message(send_from,to_address, cc_list, subject, plain_text_body=body)

    send_message(message)

错误


~\AppData\Local\Continuum\anaconda3\lib\email\_policybase.py in _fold(self, name, value, sanitize)

    367             if self.max_line_length is not None:

    368                 maxlinelen = self.max_line_length

--> 369             parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))

    370         parts.append(self.linesep)

    371         return ''.join(parts)


AttributeError: 'float' object has no attribute 'encode'

如何解决这个问题?


弑天下
浏览 121回答 1
1回答

四季花海

该错误表明库在需要字符串的地方收到了一个浮点数。从您的代码中,我希望其中一个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"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python