亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 python 發送電子郵件時獲取“浮動”對象沒有屬性“編碼”

從 python 發送電子郵件時獲取“浮動”對象沒有屬性“編碼”

弒天下 2023-03-22 17:07:22
當我從 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 messagedef 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 "[email protected]"    cc_list =   row['cc_list']    send_from="[email protected]"    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'如何解決這個問題?
查看完整描述

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]"


查看完整回答
反對 回復 2023-03-22
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號