Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 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 Answer

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'])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...