[英]Why am I getting TypeError: 'module' object is not callable in python?
[英]Why am I getting object is not callable?
编码:
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
您的Python dict
语法已关闭。 假设您要访问data
的rain
密钥,则必须使用[]
运算符。
import json
data=json.load(open("data.json"))
print(data)
data["rain"] # <--- change this
Python认为你正在调用一个函数(using ()
),因此告诉你一个字典不能被称为函数!
以下两个选项
import json
with open("data.json") as f:
data = json.load(f)
rain = data["rain"] # option 1 assuming you are sure that "rain" is in the dict
rain = data.get("rain","no rain for you") # option 2 if you are not sure that "rain" is in the dict
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.