关于python:为什么这段代码会产生这个错误?

Why this code piece is producing this error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    # -*- coding: utf-8 -*-
    from __future__ import print_function
    import io
    import os
    from os.path import dirname, join

    from alembic import command
    from alembic.config import Config
    import baker

    from webapp.database import db
    from webapp.main import app
    import webapp.areas.manage
    import webapp.users.manage
    import webapp.planner.manage
    import webapp.tests.manage


    @baker.command
    def load_data():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    traceback (most recent call last):
      File"manage.py", line 11, in
        from webapp.database import db
      File"/var/www/project01/webapp/database/__init__.py", line 3, in
        from .main import db
      File"/var/www/project01/webapp/database/main.py", line 6, in
        from ..app import app
      File"/var/www/project01/webapp/app.py", line 37, in
        default_timezone="America/Lima"
      File"/usr/local/lib/python2.7/dist-packages/allspeak/allspeak.py", line 30, in __init__
        super(Allspeak, self).__init__(*args, **kwargs)
      File"/usr/local/lib/python2.7/dist-packages/allspeak/i18n.py", line 33, in __init__
        self.load_translations()
      File"/usr/local/lib/python2.7/dist-packages/allspeak/i18n.py", line 50, in load_translations
        self.translations = self.reader.load_translations(locales=locales)
      File"/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 129, in load_translations
        self._update_translations(translations, filepath)
      File"/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 96, in _update_translations
        data = self._load_file(filepath)
      File"/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 90, in _load_file
        return self._extract_locales(data)
      File"/usr/local/lib/python2.7/dist-packages/allspeak/reader.py", line 81, in _extract_locales
        for locale, trans in data.items()
    AttributeError: 'NoneType' object has no attribute 'items'


1
2
3
4
5
6
7
8
File"/var/www/comunidadsm/env/local/lib/python2.7/site-packages/allspeak/reader.py", line 79, in _extract_locales
for locale, trans in data.items()
AttributeError: 'NoneType' object has no attribute 'items'

Perhaps changing that line to:
for locale,trans in (data.items() if data else [])

Not sure if that syntax will work right here, but it is an attempt

若要在数据不是"无"时传递空列表,请跳过循环。也许不是最好的解决方案,但你可以找出在它之后。


如果没有代码,我只能猜测这一点。属性错误表示您试图访问不存在的对象的属性。

例如,如果我有一个名为矩形的对象。它有两个属性宽度和长度;现在,如果我尝试使用这个矩形并尝试访问一个名为radius的属性,它将产生一个错误,因为该对象的属性不存在。