python - 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()
解决方案
推荐阅读
- javascript - 推送新对象时未定义状态值
- python - 获取关键字错误。也许是尾随空格
- python - __getattribute__ 如何获取值?
- sql - 将服务器身份验证设置为 SQL Server 和 Windows 身份验证模式的命令
- unit-testing - 无法导入 kotlin.test 以使用 assertFailsWith
- c++ - 为什么 C++ 标准会以这种方式处理文件?
- azure-machine-learning-service - 是否可以加快本地 AzureML 模型部署?
- java - 自定义比较器不排序时间
- css - 使用 javascript 编辑 iframe 中元素的 css
- c# - C#“while”应该在有人输入正确答案后中断,但它没有