【问题标题】:'Nonetype' object is not subscriptable - From outlook API extraction'Nonetype' 对象不可下标 - 从 Outlook API 提取
【发布时间】:2021-11-23 16:28:15
【问题描述】:

我有一些代码可以直接连接到 Outlook 并从收件箱中提取元数据,然后编译成 pandas。但是,我似乎遇到了一个以前从未遇到过的错误,我怀疑它与 Outlook 提取中的错误数据(即电子邮件中的空白数据)有关。但我似乎无法隔离。有人见过这个吗?

ERROR:app:Exception on /ctplive [GET]
Traceback (most recent call last):
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\programdata\anaconda\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\programdata\anaconda\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\programdata\anaconda\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\ADM\Code Projects\web_data_connector\outlook_api\app.py", line 100, in mailbox_insights
    inbox['Conversation Length'] = determine_conversation_length(inbox['ConversationIndex'], archive['ConversationIndex']).astype(int).values
  File "c:\users\adm\code projects\ccf_email_automation\ccf_email_automation\insights.py", line 92, in determine_conversation_length
    res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
  File "c:\users\adm\code projects\ccf_email_automation\ccf_email_automation\insights.py", line 92, in <listcomp>
    res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
TypeError: 'NoneType' object is not subscriptable

这是来自 app.py 的块

    if mailbox not in ['ctp', 'csu', 'dmt']:
        return 404
    archive, inbox = get_emails(mailbox, get_outlook())
    inbox['Sender'] = inbox[['SenderName', 'SenderEmailAddress']].apply(lambda x: read_email_address(*x), axis=1)
    inbox['Sender Domain'] = inbox['SenderEmailAddress'].apply(lambda x: x[x.index('@') + 1:] if '@' in x else 'gilead.com')
    inbox['TABL_Highest Volume Senders'] = top_senders(inbox)
    inbox['TABL_Highest Volume Sender Domains'] = top_sender_domains(inbox)
    *************line 100 starts here**************
    inbox['Conversation Length'] = determine_conversation_length(inbox['ConversationIndex'], archive['ConversationIndex']).astype(int).values
    inbox['TABL_Longest Conversations'] = longest_conversations(inbox['Conversation Length'])
    holiday_list = get_holidays()
    inbox['Business Day SLA'] = inbox[['Received Date', 'Flag Completed Date']].apply(lambda x: calculate_sla(*x, holiday_list=holiday_list), axis=1)
    inbox['Team Member'] = hardcode_team_member(inbox, mailbox)
    inbox['Sentiment Score'] = determine_sentiment(inbox['Body'])
    inbox['Flag Status'] = inbox[['Flag Completed Date', 'FlagRequest']].apply(lambda x: flag_status(*x), axis=1)
    inbox['Complexity'] = inbox['Categories'].str.extract(r'Complexity Level (\d)').astype(float)
    inbox['Protocol'] = inbox.pipe(identify_first_protocol)
    inbox['Speed to Market'] = inbox['Categories'].str.contains('Speed to Market')
    return inbox.rename(columns=lambda x: x.replace(' ', '_')).drop(columns=['ConversationIndex']).to_json(), 200

这是来自insights.py的块:

    res = pd.Series(index=inbox_index, dtype='object')
    for index in tqdm(inbox_index):
    *******Line 92 starts here********
        res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
    return res

【问题讨论】:

  • 你介意分享代码伙伴吗?
  • resx 不是列表(或其他可下标类型)。
  • @LibbyLebyane 对此感到抱歉,我已经添加了错误中引用的块。希望澄清,提前谢谢
  • 您是否在调试器中单步执行代码以查看此时所有变量的值?你确定pd.Series(index=inbox_index, dtype='object') 正在返回一些东西吗?如果你还没有做基本的调试,请做。 PyCharm 是免费的,并且有一个很棒的调试器:jetbrains.com/help/pycharm/…

标签: python anaconda outlook-restapi


【解决方案1】:

通常,在使用 API 时,建议使用if statements,因为您可能不确定会返回什么。也许您可以尝试检查inboxres 是什么类型,因此您的代码可能如下所示:

对于insights.py

res = pd.Series(index=inbox_index, dtype='object')
if res: # Check if res actually has a value before subscribing to it with res[key]
    for index in tqdm(inbox_index):
    *******Line 92 starts here********
        res[index] = len([x[:len(index)] for x in list(inbox_index) + list(archive_index) if x[:len(index)] == index])
        return res

那么你不妨在使用inbox 变量之前对app.py 执行相同的操作,或者在访问数据点之前使用一堆if statements 检查然后处理错误,如果某些东西是类型@ 987654328@.

【讨论】:

  • hmmm,当我这样做时,它会将整个 df 返回为 null,所以我认为它不会在该 if 下审查任何内容。我将添加此问题仅在几天前开始,并且已经工作了很长时间。所以我怀疑它只是数据集中的一个值,但我不知道如何隔离
  • 看起来你对你的问题有一些方向,至少你知道问题可能出在哪里。
猜你喜欢
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2015-01-23
相关资源
最近更新 更多