'If object is None'行为不符合预期



使用 python 为 facebook 聊天编写分析脚本。我正在使用的库 (fbchat( 有时会返回 None 类型对象来代替消息。我以为我可以用一个简单的

   if message is None:
      continue

在我的循环中浏览我的消息列表。但是,由于对象是 None 类型,我仍然收到异常。我在这里做错了什么?

代码片段:

for message in messages:
            #sometimes the fbchat library doesn't return a message
            if message is None:
                continue
            #strip the line endings to not mess up the csv
            message.text.replace('n', ' ')

确切的例外:

Traceback (most recent call last):
  File "collect.py", line 58, in <module>
    message.text.replace('n', ' ')
AttributeError: 'NoneType' object has no attribute 'replace'

检查message.text is None可以解决问题,这是我首先应该做的。

最新更新