简体   繁体   English

如何从python中的json列表中提取特定数据

[英]how to extract particular data from json list in python

I want to extract particular element from json list and I get my result from the code below: 我想从json列表中提取特定元素,然后从以下代码中获取结果:

result = CF.face.detect(img_url)

And when I print result I get: 当我打印结果时,我得到:

[{u'faceRectangle': {u'width': 246, u'top': 196, u'height': 246, u'left': 113}}, {u'faceRectangle': {u'width': 217, u'top': 213, u'height': 217, u'left': 614}}]

Now if i want to get width of first faceRectangle I write: 现在,如果我想获取第一个faceRectangle的宽度,我会写:

print result['facerectangle']

and I recieve 我收到了

TypeError: list indices must be integers, not str

How to fix it? 如何解决?

you result type is list so you need, get first element by index start from 0, and then you will have the dict and can get value by key: 您的result类型是list因此您需要按索引从0开始获取第一个元素,然后将获得dict并可以通过键获取值:

print type(result)
print type(result[0])

print result[0]['faceRectangle']

如果需要第一个faceRectangle的宽度,则必须获取列表result[0]的第一个元素,然后是faceRectangle键result[0]['faceRectangle'] ,最后是宽度键result[0]['faceRectangle']['width']

print result[0]['faceRectangle']['width']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM