获得';浮动';对象没有属性';编码';从python发送电子邮件时

获得';浮动';对象没有属性';编码';从python发送电子邮件时,python,pandas,dataframe,boto3,Python,Pandas,Dataframe,Boto3,当我从python发送电子邮件时,收到一个错误“float”对象没有属性“encode”。 该系统在过去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

当我从python发送电子邮件时,收到一个错误“float”对象没有属性“encode”。 该系统在过去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'

如何修复此问题?

错误表示库接收到一个浮点值,而它需要一个字符串。从您的代码中,我希望
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
在Python代码中转换为布尔值时被视为
True
。因此
to_address
赋值不会回退到
表达式(如果为NaN)。因此,如果有意义的话,你可以选择
combine_first
相关列(
final_email['fba_to_通知')。combine_first(final_email['lsp_escalation_back_'])。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"

你能提供打印(消息)
的结果吗?我想,这里有一个浮点数,而不是
str
?我的身体不是浮点数。当两行都是
NaN时,当行['fba\u to\u notifications']或行['lsp\u escalation\u back\u']或”“没有_address@rs-components.com“
行代码工作不正常