以这本词典为例:
{
"Buildings": {
"Building": [
{
"ID": "1000201",
"Name": "HARTWIG MARIE DOROTHY ADMINISTRATION BUILDING",
"Abbreviation": "HARTW",
"Campus": "Stephen M Ross Athletic Campus"
},
{
"ID": "1000700",
"Name": "CRISLER CENTER",
"Abbreviation": "CRIS",
"Campus": "Stephen M Ross Athletic Campus"
},
包含ID,名称等的词典列表仍在继续。 我试图用for循环访问这些内部词典。 campus_cds是函数get_buildings()为了提供上述信息而使用的16个数字的列表。
for campus_cd in campus_cds:
buildings = get_buildings(campus_cd)
building = buildings['Buildings']
bldg = building['Building']
所以,我能够使用#34; building = buildings [' Buildings']"
的代码行进入第二本字典"Building": [
{
"ID": "1000201",
"Name": "HARTWIG MARIE DOROTHY ADMINISTRATION BUILDING",
"Abbreviation": "HARTW",
"Campus": "Stephen M Ross Athletic Campus"
},
但是,当我尝试做" bldg =建设['建设']"时,我收到错误" TypeError:' NoneType'对象没有属性' __ getitem __'"
我尝试做的只是访问密钥' Building'的值,这是一个列表。为什么这不起作用?
在我能够访问此词典列表之后,我打算遍历列表,将给定变量与“名称”进行比较。在每个字典中找到一个匹配。