Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

python - TypeError:'int'对象不可迭代如何解析python dict(TypeError: 'int' object is not iterable how to parse python dict)

mydict =  
{ 
'ServiceResult': { 
'msgBody': { 
  'itemList': [{ 
    'busRouteId': '100100016', 
    'busRouteNm': '110A', 
    }, { 
    'busRouteId': '100100015', 
    'busRouteNm': '110B', 
    }, { 
    'busRouteId': '165000146', 
    'busRouteNm': '1100', 
    }, { 
    'busRouteId': '165000147', 
    'busRouteNm': '1101', 
    }, { 
    'busRouteId': '218000011', 
    'busRouteNm': '1100', 
    }, { 
    'busRouteId': '222000074', 
    'busRouteNm': '1100', 
    }, { 
    'busRouteId': '235000085', 
    'busRouteNm': '1100', 
    }, { 
    'busRouteId': '234000879', 
    'busRouteNm': '1101', 
    }, { 
    'busRouteId': '204000082', 
    'busRouteNm': 'G8110', 
    }] 
 } 
} 
} 

i want to print all of itemList's 'busRouteId' so i write a code

(我想打印所有itemList的“ busRouteId”,所以我写了一个代码)

for i in len(mydict['ServiceResult']['msgBody']['itemList']): 
print(mydict['ServiceResult']['msgBody'][i]['busRouteId']) 

but it doesn't work and just see this error : TypeError: 'int' object is not iterable how to parse python dict how can i modify it??

(但它不起作用,只是看到此错误:TypeError:'int'对象不是可迭代的如何解析python dict我如何修改它?)

  ask by student translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use range() with len() , and you need to use the 'itemList' inside the loop as well

(您需要将range()len()一起使用,并且还需要在循环内使用'itemList')

 for i in range(len(mydict['ServiceResult']['msgBody']['itemList'])):
    print(mydict['ServiceResult']['msgBody']['itemList'][i]['busRouteId'])

Or simply iterate over the items in 'itemList'

(或者只是迭代'itemList'的项目)

 for item_list in mydict['ServiceResult']['msgBody']['itemList']:
    print(item_list['busRouteId'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...