【问题标题】:dict object is not callable [duplicate]dict 对象不可调用[重复]
【发布时间】:2015-06-05 14:47:16
【问题描述】:

这是我的 python 程序,它与 mongodb* 建立连接

import pymongo
from pymongo import MongoClient
connection = MongoClient('localhost', 27017)
db = connection.test
names = db.names
item =names.find_one()
print item('name')

而且,我收到了这个错误

C:\Users\hpg6\Desktop>python mycon.py
Traceback (most recent call last):
  File "mycon.py", line 15, in <module>
    print (item('name'))
TypeError: 'dict' object is not callable

【问题讨论】:

    标签: python mongodb bottle


    【解决方案1】:

    这一行

    print item('name')
    

    应该是

    print(item['name'])
    

    如果itemdict,它认为您正在尝试调用函数(),而不是使用键[] 查找值

    【讨论】:

    • 如果是 Python 2,那么它是 print item['name'],对吗? OP 并未表明他正在使用 Python 3。
    • OP 评论了我的帖子,指出他们正在使用 python 3(尽管他们现在删除了该评论)
    【解决方案2】:

    你用的是圆括号,试试print (item['name'])

    【讨论】:

      猜你喜欢
      • 2017-02-11
      • 2019-08-10
      • 2011-10-01
      • 2016-07-07
      • 2020-09-03
      • 2012-10-31
      • 2017-05-27
      • 2016-03-07
      相关资源
      最近更新 更多