首页 > 解决方案 > 为什么我得到对象不可调用?

问题描述

编码:

import json
data=json.load(open("data.json"))
print(data)
data("rain")

给我一个错误:

Traceback (most recent call last):  
File "<stdin>", line 1, in <module> TypeError: 'dict' object is not callable

标签: pythonpython-3.x

解决方案


您的 Pythondict语法已关闭。假设您要访问 的rain密钥data,则必须使用[]运算符。

import json
data=json.load(open("data.json"))
print(data)
data["rain"]  # <--- change this

Python 认为您正在调用一个函数(使用()),因此告诉您不能将字典作为函数调用!


推荐阅读