【问题标题】:AttributeError: 'list' object has no attribute 'strip' yagmail and csvAttributeError: 'list' 对象没有属性 'strip' yagmail 和 csv
【发布时间】:2019-02-05 22:51:13
【问题描述】:

这个问题问了数百次,但我仍然无法解决我自己的问题。当我运行 py 脚本时,我得到这个错误作为回报。

目前当我运行脚本时出现错误。

Traceback(最近一次调用最后一次): 文件“/Users/admin/Desktop/CIB2019/Scripts/testarray.py”,第 10 行,在 对于 line.strip().split(',') 中的姓名、电子邮件、消息: AttributeError: 'list' 对象没有属性 'strip'

**Code Contents

import yagmail
import csv

wait = 15 #seconds in between messages

yag = yagmail.SMTP('email@gmail.com', 'password')

with open("mails.csv", "r") as csvfile:
    csv_reader = csv.reader(csvfile, delimiter=',')
    next(csv_reader)
    for line in csv_reader:
        for name, email, message in line.strip().split(','):
            yag.send(email, subject = name, contents = message)
            print("sent")
            time.sleep(wait) 





**Mails.csv Contents

Name,Email,Message  
a,4236543542@txt.att.net,Meow1
b,9876543542@txt.att.net,Meow2

登录 gmail smtp,打开 mails.csv,向每个用户发送邮件,在发送给每个用户之间等待 15 秒。

【问题讨论】:

    标签: python


    【解决方案1】:

    csv.reader 生成器已经为您解析了这些行,因此您不需要自己处理 strip 和 split 行:

    for name, email, message in csv_reader:
        yag.send(email, subject = name, contents = message)
        print("sent")
        time.sleep(wait) 
    

    【讨论】:

    • 现在可以使用了。谢谢你。他们有这么多关于它的主题和答案。
    猜你喜欢
    • 2013-07-19
    • 1970-01-01
    • 2017-05-03
    • 2020-03-25
    • 2016-08-11
    • 2013-07-18
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多