Python的新特性:“ NoneType”类型的参数不可迭代

时间:2019-06-22 15:53:39

标签: python

我是Python的新手,它是第一个分配给并且现在需要支持的项目:) Python似乎是超级友好的语言。所以请原谅我愚蠢的问题。

我遇到以下错误:

  File "AlKamelClient.py", line 316, in <module>
    client.main()
  File "AlKamelClient.py", line 296, in main
    self.dispatch_reply_message(self.parse_msg(msg))
  File "AlKamelClient.py", line 226, in dispatch_reply_message
    self.log_to_db(message)
  File "AlKamelClient.py", line 181, in log_to_db
    if 'session' in data['timing']:
TypeError: argument of type 'NoneType' is not iterable

在下面的代码中,第181行是“如果data ['timing']:中的'session'。”

我看过尝试:捕获异常等。我不确定从这里去哪里。

代码是:

def log_to_db(self, message):

    channel = message['channel']
    data = message['data']

    if 'timing' in data:
        if 'session' in data['timing']:
            if 'info' in data['timing']['session']:
                pp.pprint(data['timing']['session']['info'])
                self._timing_session_info.update(data['timing']['session']['info'])
                self._session_id = db.timing_session_info(self._db.cursor(), db.NoneDict(self._timing_session_info))

            if 'entry' in data['timing']['session']:
                pp.pprint(data['timing']['session']['entry'])
                db.timing_session_entry(self._db.cursor(), db.NoneDict(data['timing']['session']['entry']), self._session_id)

            if 'flags' in data['timing']['session']:
                pp.pprint(data['timing']['session']['flags'])
                db.timing_session_flags(self._db.cursor(), db.NoneDict(data['timing']['session']['flags']), self._session_id)

        if 'liveEvents' in data['timing']:
            if 'fl' in data['timing']['liveEvents']:
                pp.pprint(data['timing']['liveEvents']['fl'])
                db.timing_liveEvents_fl(self._db.cursor(), data['timing']['liveEvents']['fl'], self._session_id)
            if 'sector' in data['timing']['liveEvents']:
                pp.pprint(data['timing']['liveEvents']['sector'])
                db.timing_liveEvents_sector(self._db.cursor(), db.NoneDict(data['timing']['liveEvents']['sector']), self._session_id)

    if 'telemEvent' in data:
        pp.pprint(data['telemEvent'])
        db.telemEvent(self._db.cursor(), db.NoneDict(data['telemEvent']), self._session_id)

1 个答案:

答案 0 :(得分:2)

该错误表示未设置data['timing'],即设置为null / None。

声明:

if 'session' in data['timing']:

实际上在data['timing']上进行迭代,但是由于它为null / None,因此无法对其进行迭代。您应该检查一下它是否真的设置好。