首页 > 解决方案 > TypeError: 'float' 类型的对象没有 len(): 在尝试从 Excel 表向某些地址发送电子邮件时得到这个

问题描述

尝试从 Excel 工作表向某些地址发送电子邮件时出现错误。这是我的代码。电子邮件列是一个系列,那么我该如何迭代它?另一件事是我可能在 for 循环条件下收到错误

追溯

回溯(最近一次通话最后):

文件“F:\Labelster-20211103T080644Z-001\Labelster\email_send.py”,第 76 行,在 server.sendmail(your_email,[email],message)中

文件“C:\Users\Vcube\anaconda3\lib\smtplib.py”,第 876 行,在 sendmail (code, resp) = self.rcpt(each, rcpt_options)

文件 "C:\Users\Vcube\anaconda3\lib\smtplib.py",第 546 行,在 rcpt self.putcmd("rcpt", "TO:%s%s" % (quoteaddr(recip), optionlist))

文件“C:\Users\Vcube\anaconda3\lib\smtplib.py”,第 150 行,在 quoteaddr 显示名称中,addr = email.utils.parseaddr(addrstring)

文件“C:\Users\Vcube\anaconda3\lib\email\utils.py”,第 212 行,在 parseaddr addrs = _AddressList(addr).addresslist

文件“C:\Users\Vcube\anaconda3\lib\email_parseaddr.py”,第 509 行,在init self.addresslist = self.getaddrlist()

文件“C:\Users\Vcube\anaconda3\lib\email_parseaddr.py”,第 252 行,在 getaddrlist 中,而 self.pos < len(self.field):

TypeError:“float”类型的对象没有 len()

# import the required libraries
import pandas as pd
import smtplib
  
# change these as per use
your_email = "**************"
your_password = "****************"
  
# establishing connection with gmail
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(your_email, your_password)
  
# reading the spreadsheet
email_list = pd.read_excel('F:/Automate Email/test_email.xlsx')
  
# getting the names and the emails
names = email_list['Name']
emails = email_list['Email']
#emails = emails.to_string()

print(type(emails))


monthly_salary = email_list['Monthly Salary']
# iterate through the records
for i in range(emails.size):
  
    # for every record get the name and the email addresses
    name = names[i]
    email = emails[i]
    monthly_salary = monthly_salary[i]
        
    
    # the message to be emailed
    #message =  "Monthly Salary:" , monthly_salary
    message = """Subject: Hi {name}, your monthly salary is {monthly_salary}"""
  
    # sending the email
    server.sendmail(your_email, [email], message)
  
# close the smtp server
server.close()

标签: pythonexcelemail

解决方案


推荐阅读