首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'AttributeError‘对象没有'To’属性

AttributeError:'AttributeError‘对象没有'To’属性
EN

Stack Overflow用户
提问于 2021-01-14 21:41:21
回答 1查看 80关注 0票数 0

我正在尝试使用python Jupyter notebook探索安然电子邮件数据集。但是我得到了这个属性错误。我正在尝试阅读电子邮件,并将它们转换为csv格式,以便我可以进一步应用Ml进行情绪分析。导入tarfile从datetime导入re从集合导入datetime导入namedtuple,计数器导入pandas as pd导入altair as alt

代码语言:javascript
复制
tar =tarfile.open(r"C:\Users\nikip\Documents\2021\Interview Preparation\sentiment analysis\enron_mail_20150507.tar.gz", "r")
items = tar.getmembers()
Email = namedtuple('Email', 'Date, From, To, Subject, Cc, Bcc, Message')

def get_msg(item_number):
    f = tar.extractfile(items[item_number])
    try:
        date = from_ = to = subject = cc= bcc = message= ''
        in_to = False
        in_message = False
        to = []
        message = []
        item = f.read().decode()
        item = item.replace('\r', '').replace('\t', '')
        lines = item.split('\n')
        
        for num, line in enumerate(lines):
            if line.startswith('Date:') and not date:
                date = datetime.strptime(' '.join(line.split('Date: ')[1].split()[:-2]), '%a, %d %b %Y %H:%M:%S')
            elif line.startswith('From:') and not from_:
                from_ = line.replace('From:', '').strip()                
            elif line.startswith('To:')and not to:
                in_to = True
                to = line.replace('To:', '').replace(',', '').replace(',', '').split()                
            elif line.startswith('Subject:') and not subject:
                in_to = False
                subject = line.replace('Subject:', '').strip()                
            elif line.startswith('Cc:') and not cc:
                cc = line.replace('Cc:', '').replace(',', '').replace(',', '').split()                
            elif line.startswith('Bcc:') and not bcc:
                bcc = line.replace('Bcc:', '').replace(',', '').replace(',', '').split()                
            elif in_to:
                to.extend(line.replace(',', '').split())                
            elif line.statswith('Subject:') and not subject:
                in_to =False                
            elif line.startswith('X-FileName'):
                in_message = True                
            elif in_message:
                message.append(line)
        
        to = '; '.join(to).strip()
        cc = '; '.join(cc).strip()
        bcc = '; '.join(bcc).strip()
        message = ' '.join(message).strip()
        email = Email(date, from_, to, subject, cc, bcc, message)
        return email
    
    except Exception as e:
        return e

msg = get_msg(3002)
msg.date

我收到如下错误消息:

代码语言:javascript
复制
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-e1439579a8e7> in <module>
----> 1 msg.To

AttributeError: 'AttributeError' object has no attribute 'To'

有人能帮上忙吗?提前谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-01-14 21:50:52

问题是您在get_msg函数中返回了一个异常,大致如下所示:

代码语言:javascript
复制
def get_msg(item_number):
  try:
    ...do some stuff...
  except Exception as e:
    return e

看起来您在代码中的某处触发了AttributeError异常,并且返回的是该异常,而不是Email对象。

您几乎永远不会希望有一条except语句来抑制所有这样的异常,因为它会隐藏代码中的错误(正如我们在这里看到的)。通常更好的做法是捕获特定的异常,或者至少记录错误,如果您的代码将在异常的情况下继续执行。

作为第一步,我建议删除整个try/except块并让代码在没有它的情况下工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65720105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档