Python词典“没有属性'__getitem__'

时间:2014-04-17 00:00:24

标签: python python-2.7 dictionary

Python的新手,似乎找不到这个答案。我正在尝试使用字典,如哈希或其他语言的关联数组,不确定这是否正确。

现在我正在做:

data['person'] = {'birthday': '01-01-1980'}

修改:data是从dict

返回的json.loads(response.text)

这会引发object has no attribute '__getitem__'错误。在SO上查看这个告诉我,我需要从我的 unicode 方法返回一个字符串,但这些只是简单的旧词典,没什么特别的。

Python v2.7.4

Request Method: GET
Request URL:    http://localhost:8000/url/returning/json
Django Version: 1.5.2
Exception Type: TypeError
Exception Value:    
'Person' object has no attribute '__getitem__'
Exception Location: /path/to/error/file in next_work_item, line 356
Python Executable:  /path/to/project/.venv/bin/python
Python Path:    
['/path/to/project/',
 '/opt/pycharm-3.1.1/helpers/pydev',
 '/path/to/project/.venv/local/lib/python2.7/site-packages/distribute-0.7.3-py2.7.egg',
 '/path/to/project/.venv/lib/python2.7/site-packages/distribute-0.7.3-py2.7.egg',
 '/path/to/project/',
 '/path/to/project/.venv/lib/python2.7',
 '/path/to/project/.venv/lib/python2.7/plat-x86_64-linux-gnu',
 '/path/to/project/.venv/lib/python2.7/lib-tk',
 '/path/to/project/.venv/lib/python2.7/lib-old',
 '/path/to/project/.venv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/path/to/project/.venv/local/lib/python2.7/site-packages',
 '/path/to/project/.venv/lib/python2.7/site-packages',
 '/path/to/project/.venv/local/lib/python2.7/site-packages/IPython/extensions',
 '/path/to/project/sub/project,
 '/path/to/project/sub']

1 个答案:

答案 0 :(得分:1)

这很好用:

#!/usr/local/cpython-2.7/bin/python

data = {}
data['person'] = {'birthday': '01-01-1980'}

...所以数据可能不是你的情况下的字典。 SSCCE可能会有所帮助。